2013-02-25 86 views
0

我有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 「}, 」提交「=>」 創建選民「}

所以什麼問題呢?

+0

我們需要看到的是處理表單的POST的控制器代碼。 – nathan 2013-02-25 18:56:06

回答

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