2

我已经在我的用户模型(设计)中添加了一个“头像”并且收到了错误Can't mass-assign protected attributes: avatar

在我的用户模型中,我添加了:

attr_accessible :avatar_uid, :avatar_name
image_accessor :avatar

表单 (simple_form) 有 :html => { :method => :put, :multipart => true } 和字段:

<%= f.file_field :avatar %>
<%= f.hidden_field :retained_avatar %>
<%= f.check_box :remove_avatar %>

添加:avatarattr_accessible解决批量分配错误,但字段只是没有保存在用户表中。

4

1 回答 1

0

You need to add avatar in attr_accessible list as well in order to mass-assign it.

So just replace attr_accessible list by,

 attr_accessible :avatar_uid, :avatar_name, :avatar

image_accessor is provided by dragon-fly gem which provide reader/writer for listed attribute like attr_accessor.

check this out this thread to know more differnce between attr_accessible and attr_accessor

"WARNING: Can't mass-assign protected attributes"

于 2013-04-08T09:54:29.303 回答