0

我有 3 个模型。首先,我有一个有很多选票的选民。Votes 是一个连接表,用于链接投票者和条目。但是当我试图保存选票时,他们并没有保存。我的模型如下所示:

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=", "voter"=>{"email_address"=>"sadasfd"}, "votes"=>{"entry_id "=>"3"}, "commit"=>"创建选民"}

那么怎么了?

4

1 回答 1

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
于 2013-08-22T06:39:37.677 回答