0

I am new to Ruby on Rails.

I am using Rails 4.0.3, Ruby 1.9.3.

I tried to import CSV file from the sample "396-importing-csv-and-excel-master" But it is throwing error.

Error:

attr_accessible is no longer in use

and suggests to use Strong parameter. Can any one help me to import CSV using strong parameter?

Charles
  • 50,943
  • 13
  • 104
  • 142

2 Answers2

1

Let's say you are importing Tasks. Use this way for strong parameters

def self.import(file)    
  CSV.foreach(file.path, headers: true) do |row|
    task = find_by_id(row["id"]) || new
    parameters = ActionController::Parameters.new(row.to_hash)
    task.update(parameters.permit(:id,:name))
    task.save!
  end
end
srbhattarai
  • 792
  • 7
  • 9
  • I know this one is old, but it was helpful. What puzzles me here is why the import code goes in the model rather than in the controller with the other actions that create or update from user input? – dlu Apr 17 '19 at 21:01
0

attr_accessible is no longer use in rails 4. In rails4 just like params.require(:person).permit(:name, :age)

I think this can help you

alex fang
  • 66
  • 2