收到批量分配警告但不知道为什么
我对以下有关批量分配的警告感到非常困惑:
WARNING: Can't mass-assign protected attributes: upload_id
这是我的上传模型:
class Upload < ActiveRecord::Base
belongs_to :uploadable, :polymorphic => true
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x150>', :large => '300x300>'}
这是我的用户模型
class User < ActiveRecord::Base
has_one :upload, :as => :uploadable
attr_accessible :name, :email, :password, :password_confirmation, :birthdate, :emails, :icon_id
模型中没有 :upload_id。
在控制器更新操作中:
def update
@user.upload = Upload.find_by_id(params[:user][:upload_id])
respond_to do |format|
if @user.update_attributes(:user)
format.js
end
end
end
任何人都可以告诉我为什么会出现此错误。该应用程序可以运行,但我想解决这个问题。
I am very confused by the following warning about mass assignment:
WARNING: Can't mass-assign protected attributes: upload_id
Here is my uploads model:
class Upload < ActiveRecord::Base
belongs_to :uploadable, :polymorphic => true
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x150>', :large => '300x300>'}
Here is my user model
class User < ActiveRecord::Base
has_one :upload, :as => :uploadable
attr_accessible :name, :email, :password, :password_confirmation, :birthdate, :emails, :icon_id
There is no :upload_id in the models.
In the controller update action:
def update
@user.upload = Upload.find_by_id(params[:user][:upload_id])
respond_to do |format|
if @user.update_attributes(:user)
format.js
end
end
end
Can anyone tell why I get this error. The application works but I would like to fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在模型中,将
:upload_id
添加到attr_accessible
输入,如下所示:如果您希望
:upload_id
嵌套在下: user
在 params 哈希中,它需要被列为用户模型的可访问属性。In the model, add
:upload_id
to theattr_accessible
inputs, like so:If you want the
:upload_id
to be nested under:user
in the params hash, it needs to be listed as an accessible attribute for the user model.