RailsCasts Pro episodes are now free!

Learn more or hide this

patricksereno's Profile

GitHub User: patricksereno

Comments by

Avatar

This is because Rails 4 uses Strong Parameters checking in the controller instead of the attr_accessible method in the model.

In order to use the code in this cast for Rails 4, you'll need to replace the code snippet here:

ruby
def self.import(file)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.slice(*accessible_attributes)
    product.save!
  end
end

With something that looks like this:

ruby
def self.import(file)
  allowed_attributes = [ "id","name","released_on","price","created_at","updated_at"]
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.select { |k,v| allowed_attributes.include? k }
    product.save!
  end
end

Disclaimer: I'm not sure this qualifies as 'The Rails Way'. If someone has a more elegant solution, please post a reply.