窗体中嵌套的属性没有显示

问题描述:

我有3个模型,说一个选民有很多票,我试图让选民投票在相同的窗体上,当投票对象被创建,但我的表单中的字段没有显示。 这里是我的模型:窗体中嵌套的属性没有显示

class Voter < ActiveRecord::Base 
    attr_accessible :email_address, :verification_code, :verified, :votes_attributes 
    has_many :votes, :class_name => "Vote" 
    accepts_nested_attributes_for :votes 
end 

class Vote < ActiveRecord::Base 
    belongs_to :entry 
    belongs_to :voter 
    attr_accessible :entry, :voter, :voter_id 
end 

而在我的形式,我有:

<%= form_for(@voter) do |f| %> 
<% if @voter.errors.any? %> 
<div id="error_explanation"> 
<h2><%= pluralize(@voter.errors.count, "error") %> prohibited this voter from being saved:</h2> 

<ul> 
<% @voter.errors.full_messages.each do |msg| %> 
<li><%= msg %></li> 
<% end %> 
</ul> 
</div> 
<% end %> 

    <div class="field"> 
    <%= f.label :email_address %><br /> 
<%= f.text_field :email_address %> 
    </div> 
    <div class="field"> 
    <%= f.label :verification_code %><br /> 
    <%= f.text_field :verification_code %> 
    </div> 
    <div class="field"> 
    <%= f.label :verified %><br /> 
    <%= f.check_box :verified %> 
    </div> 

<div class="field"> 
     <% f.fields_for :votes do |builder| %> 
     <fieldset> 
     <%= builder.label :votes, "Entry" %> 
     <%= collection_select(:entry, :entry_id, Entry.all, :id, :email_address, :prompt => 'Please select an Entry') %> 
     <% end %> 
    </div> 
    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

但投票现场没有展示。我不明白为什么。

+0

可能的重复[获取字段\ _for使用具有\ _many关系](http://*.com/questions/9459002/getting-fields-for-to-work-with-has-many-relationship) – 2013-03-03 10:01:39

在Rails 3,您需要使用:

<%= f.fields_for :votes do |builder| %> 

这将解决这个问题。