【问题标题】:Can't mass-assign protected attributes: even if attr_accessible already added无法批量分配受保护的属性:即使 attr_accessible 已经添加
【发布时间】:2013-01-30 17:30:40
【问题描述】:

即使已添加 attr_accessible 属性,也无法批量分配 :title:url:about .在 Rails 控制台上可以,但在在线表单上不行。

帖子模型:

class Post < ActiveRecord::Base
    attr_accessible :about, :downv, :names, :points, :title, :upv, :url, :user_id

    belongs_to :user

end

用户模型:

class User < ActiveRecord::Base
    attr_accessible :email, :password_digest, :post_id, :password, :password_confirmation, :name

    has_many :posts
    has_secure_password

    validates_presence_of :password, :on => :create
end

创建后控制器:

def create
    @post = User.new(params[:post])
    @post.upv, @post.downv, @post.points = 0, 0, 0

    @post.user_id = params[:user_id]
    @post.names = ""

    if @post.save
      redirect_to root_url, notice: "Post created."
    else
      render "new"
    end
end

我的表单视图与任何其他表单视图一样。

【问题讨论】:

    标签: attr-accessible


    【解决方案1】:

    我输入的是 User.new,而不是 Post.new,已解决!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      相关资源
      最近更新 更多