0
我有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 %>
但投票現場沒有展示。我不明白爲什麼。
可能的重複[獲取字段\ _for使用具有\ _many關係](http://stackoverflow.com/questions/9459002/getting-fields-for-to-work-with-has-many-relationship) – 2013-03-03 10:01:39