我有3个型号。首先,我有一个有很多选票的选民。投票是连接选民和参赛者的联接表。但是当我试图保存他们没有保存的选票时。我的模型看起来像这样:
class Vote < ActiveRecord::Base
belongs_to :entry
belongs_to :voter
attr_accessible :entry, :voter, :voter_id
class Voter < ActiveRecord::Base
attr_accessible :email_address, :verification_code, :verified, :votes_attributes, :votes
has_many :votes, :class_name => "Vote"
accepts_nested_attributes_for :votes
class Entry < ActiveRecord::Base
attr_accessible :caption, :email_address, :filename
end
然后我的表格看起来像这样:
<%= f.fields_for :votes do |builder| %>
<fieldset>
<%= builder.label :votes, "Vote" %>
<%= collection_select(:votes, :entry_id, Entry.all, :id, :caption, :prompt => 'Please select an Entry') %>
</fieldset>
<% end %>
但投票并没有保存在数据库中。响应如下:
参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“x5f85viIp / KHJKQF7DotaF3MhebARWcaLDKRbcZw / lM =”,“选民”=&gt; {“email_address”=&gt;“sadasfd”},“投票“=&gt; {”entry_id“=&gt;”3“},”提交“=&gt;”创建选民“}
所以出了什么问题?
答案 0 :(得分:0)
请尝试
class Voter < ActiveRecord::Base
attr_accessible :email_address, :verification_code, :verified, :votes
has_many :votes, :class_name => "Vote"
attr_accessible :votes_attributes,
accepts_nested_attributes_for :votes
Modify vote_params in VotesController
private
def vote_params
params.require(:vote).permit(:id, :email_address, :verification_code, :verified, votes_attributes: [:id, :name])
end