Pergunta

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
Foi útil?

Solução

This is a tip on general debugging — you could also for curiosity's sake take a look at what params is at this point or use pry

def import
  raise StandardError, params.inspect
end

I also recommend that you use Strong Parameters and I have detailed a clean approach on my blog that you may find useful: Bootstrapping Strong Parameters in Rails.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top