奇怪的!当我使用fields_for
声明嵌套属性字段时,rails 添加了一个隐藏属性,其中包含嵌套属性的 id(以执行更新):
= form_for @opinion do |f|
= f.fields_for :client do |client_f|
= client_f.text_field :name
给我:
<input name="opinion[client_attributes][name]" type="text" />
<input name="opinion[client_attributes][id]" type="hidden" value="4" />
这将导致:
Can't mass-assign protected attributes: client_attributes
当然,这是我的模型:
class Opinion < ActiveRecord::Base
attr_accessible :content
attr_accessible :client_id
validates :content, :presence => true, :length => { :maximum => 2048 }
belongs_to :client
accepts_nested_attributes_for :client
end
class Client < ActiveRecord::Base
attr_accessible :name
validates :name, :presence => true, :length => { :maximum => 64 }
has_many :opinions
end
Rails 视图有问题,还是模型有问题?
知道如何解决吗?提前致谢。