0

So I am working on my first rails app and I can't seem to get past this error. I apologize if I don't provide enough information, I am still new to this stuff.

Here is the model I am trying to use:

class Movie < ActiveRecord::Base
   attr_accessible :tilte, :rating, :description, :release_date
end

After a bit of searching, I discovered I should be able to force it to work if I disabled the white list by setting config.active_record.whitelist_attributes = false in the /app/config.rb which I did in an attempt just to bypass the issue for this assignment, yet I am STILL not able to mass assign attributes.

This is the specific error message that I am receiving: ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: title

And here is the code I am trying to execute from the rails console:

####  Create
starwars = Movie.create!(:title => 'Star Wars',
  :release_date => '25/4/1977', :rating => 'PG')
# note that numerical dates follow European format: dd/mm/yyyy
requiem =  Movie.create!(:title => 'Requiem for a Dream',
  :release_date => 'Oct 27, 2000', :rating => 'R')
#  Creation using separate 'save' method, used when updating existing records
field = Movie.new(:title => 'Field of Dreams',
  :release_date => '21-Apr-89', :rating => 'PG')
field.save!
field.title = 'New Field of Dreams'
####  Read
pg_movies = Movie.where("rating = 'PG'")
ancient_movies = Movie.where('release_date < :cutoff and rating = :rating',
  :cutoff => 'Jan 1, 2000', :rating => 'PG')
####  Another way to read
Movie.find(3)  # exception if key not found; find_by_id returns nil instead
####  Update
starwars.update_attributes(:description => 'The best space western EVER',
  :release_date => '25/5/1977')
requiem.rating = 'NC-17'
requiem.save!
####  Delete
requiem.destroy
Movie.where('title = "Requiem for a Dream"')
####  Find returns an enumerable
Movie.where('rating = "PG"').each do |mov|
  mov.destroy
end

Any advice?

3 Answers3

0

Not sure if this is just an error in your translation of the question to SO, but you have spelled "title" incorrectly in your model. Do you still get the error upon correcting this?

shannondoah
  • 90
  • 1
  • 7
0

I just run your code in two different scenarios: 1) including the line where you define your attr_accessible parameters in your model and 2) without them.

In the first case it doesn't persist any value in the DB, but it does when you remove that line. Try that.

I don't know exactly what you are trying to do there, but in the case of ActiveRecord models, getters and setters are already generated by ActiveRecord for your data columns. attr_accessor (different from attr_accessible) is not needed or desirable.

Check out the answer by colinm under this question to read about the difference between attr_accessor and attr_accessible.

Hope it helps!

Community
  • 1
  • 1
0

I did fix that typo and that fixed one issue, but I was still unable to mass assign values at all.

To fix the mass assignment issue I had to comment out config.active_record.mass_assignment_sanitizer = :strict in my config/environments/development.rb