我正在尝试对嵌套模型进行 update_attributes 并不断遇到质量分配错误。这是我的模型:
class Lineup < ActiveRecord::Base
belongs_to :user
has_many :piece_lineups
has_many :pieces, through: :piece_lineups
accepts_nested_attributes_for :piece_lineups
end
class Piece < ActiveRecord::Base
attr_accessible :cost, :description, :name, :category
has_many :piece_lineups
has_many :lineups, through: :piece_lineups
end
class PieceLineup < ActiveRecord::Base
attr_accessible :piece
belongs_to :piece
belongs_to :lineup
end
顺便说一句,用户 has_one 阵容。所以我认为通过将 Accepts_nested_attributes_for 添加到阵容模型中它会起作用,但事实并非如此。这是我的表格:
- @lineup.piece_lineups.build(:piece => piece)
= form_for(@lineup) do |f|
= f.fields_for :piece_lineups do |piece_lineup|
= piece_lineup.hidden_field(:piece_id, :value => piece.id)
= f.submit
和我的阵容控制器动作:
def update
@lineup = current_user.lineup
@lineup.update_attributes(params[:lineup])
最后,错误:
无法批量分配受保护的属性:piece_lineups_attributes
我在这里想念什么?谢谢!