0

I've searched for an answer this this on SO and haven't found it. I'm getting this error: Can't mass-assign protected attributes: Brand, Details, Product, Code

I'm uploading csv data from a file into my rails db. Here is the import action from the upload page controller:

def import
     Item.import(params[:file])
     redirect_to root_url, notice: "Products imported."
end

The attr_accessible line from Item model:

attr_accessible :brand, :details, :img, :product, :code

I'm using Devise, not sure if that is part of the problem? Do I need to do something in my User model to make this work? Thanks in advance.

Here's the self.import method from Item:

  def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
    Item.create! row.to_hash
   end
  end
4

1 回答 1

1

这是一个关于一般调试的技巧——出于好奇,你也可以看看现在params是什么或使用pry

def import
  raise StandardError, params.inspect
end

我还建议您使用强参数,并且我在我的博客上详细介绍了一种您可能会觉得有用的简洁方法:Bootstrapping Strong Parameters in Rails

于 2013-07-26T08:01:05.130 回答