无法批量分配受保护的属性:tags_attributes?

我正在尝试按照Rails指南为post创建标签:

tag.rb:

class Tag < ActiveRecord::Base attr_accessible :name belongs_to :post end 

post.rb:

 class Post  true, :length => { :maximum => 30 }, :uniqueness => true validates :content, :presence => true, :uniqueness => true belongs_to :user has_many :comments, :dependent => :destroy has_many :votes, :as => :votable, :dependent => :destroy has_many :tags accepts_nested_attributes_for :tags, :allow_destroy => :true, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } end 

意见/职位/ _form.html.erb:

    


Tags

'tags/form', :locals => {:form => post_form} %>

意见/标签/ _form.html.erb:

  

但是当我尝试创建标记时出现此错误:

无法批量分配受保护的属性:tags_attributes Rails.root:/ home / alex / rails / r7

应用程序跟踪| 框架跟踪| 完全跟踪app / controllers / posts_controller.rb:25:在’create’请求中

参数:

{“utf8”=>“✓”,“authenticity_token”=>“VF / qlfZ4Q5yvPY4VIbpFn65hoTAXdEa4fb4I1Ug4ETE =”,“post”=> {“title”=>“5号邮箱”,“内容”=>“邮编5号邮编5 post number 5“,”tags_attributes“=> {”0“=> {”name“=>”food,drinks“}}},”commit“=>”创建post“}

有什么建议来解决这个问题

Attr_accessible指定您无法批量分配属性。 在这里,您还需要将post_id设为attr_accessible。 请参阅警告:无法批量分配受保护的属性

只需输入:tags_attributes而不是:tags。 请参考下文。 这将解决我所面临的问题

 class Post < ActiveRecord::Base attr_accessible :title, :content, :tags_attributes end 

这对我有用:

 class Post < ActiveRecord::Base attr_accessible :name, :title, :content, :tags_attributes end 

这对我也有用:

 class Post < ActiveRecord::Base attr_accessible :title, :content, :tags_attributes end 

这允许您通过Post访问标签属性。