嵌套表格不保存数据
问题描述:
我有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”=> “✓”, “authenticity_token”=> “x5f85viIp/KHJKQF7DotaF3MhebARWcaLDKRbcZw/LM =”, “投票”=> { “EMAIL_ADDRESS”=>” sadasfd “}, ”票“=> {” entry_id “=>” 3 “}, ”提交“=>” 创建选民“}
所以什么问题呢?
答
请尝试
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
我们需要看到的是处理表单的POST的控制器代码。 – nathan 2013-02-25 18:56:06