【问题标题】:Can't mass assign protected attributes :post_id无法批量分配受保护的属性:post_id
【发布时间】:2012-12-02 15:56:25
【问题描述】:

我一直在尝试为我的 RoR 3 网站创建一个赞按钮。我已使用此链接中建议的解决方案:Simple like/unlike button with rails 3, jquery, and ajax

我有一个用户、帖子和点赞模型。但是喜欢按钮部分显示我的显示帖子页面中的“无法批量分配受保护的属性:post_id”错误。我已经研究了其他线程的解决方案,但似乎没有任何效果。粘贴我的代码摘录以供参考。

class Like < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
end

class Post < ActiveRecord::Base
   attr_accessible :title, :content, :posts_attributes
   belongs_to :user
   has_many :likes
   accepts_nested_attributes_for :likes
   .
   .
   .
end

class User < ActiveRecord::Base
   attr_accessible :email, :name, :password, :password_confirmation
   has_many :posts, dependent: :destroy
   has_many :likes
   .
   .
   .
end

提前致谢。

【问题讨论】:

    标签: ruby-on-rails-3.2


    【解决方案1】:

    将 attr_accessible :post_id, :user_id 添加到您的 Like 模型

    【讨论】:

    • 很高兴听到!您能帮我一个忙,选择我的答案作为最终答案吗?
    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多