-4

当对 /cloth/create 进行 POST 时,我收到警告:无法批量分配受保护的属性:标题、描述、布料类型、图片

布料.rb

class Cloth   
  include Mongoid::Document   
  include Mongoid::Timestamps 
  include Mongoid::MultiParameterAttributes   attr_accessible :pics

  field :title   
  field :description   
  field :cloth_type   
  belongs_to :seller   
  has_many :pics

  attr_accessible :pic_attributes   
  accepts_nested_attributes_for:pics
end

图片.rb

class Pic   
  include Mongoid::Document
  include Mongoid::Paperclip  

  has_mongoid_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" },
  :storage => :cloud_files,
  :cloudfiles_credentials =>  "#{Rails.root}/config/rackspace.yml",
  :path => ":attachment/:id/:timestamp_:style.:extension"

  belongs_to :cloth
  attr_accessible :cloth_attributes

  def create        
    @cloth = Cloth.create!(params[:cloth])  
  end
end
4

1 回答 1

6

答案可以在这里找到:http: //api.rubyonrails.org/classes/ActiveModel/MassAssignmentSecurity/ClassMethods.html#method-i-attr_accessible

不过,我会说明。

通过设置attr_accessible :pic_attributes,您正在为可以通过批量分配设置哪些属性设置白名单。换句话说,您是说只有您列出的属性可以通过执行诸如cloth = Cloth.new(params[:cloth]). 您所说的任何其他属性都应该使用它们的访问器来设置,例如cloth.title = params[:title].

如果您需要attr_accessible,则将其他属性添加到您的列表中。

attr_accessible :pic_attributes, :title, :description, etc ...
于 2013-01-03T15:42:01.160 回答