user3436436
user3436436

Reputation: 1

Import CSV to database using Rails 4.0.3

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?

Upvotes: 0

Views: 587

Answers (2)

srbhattarai
srbhattarai

Reputation: 802

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

Upvotes: 1

alex fang
alex fang

Reputation: 66

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

Upvotes: 0

Related Questions