mardi 24 février 2015

Using attr_accessible, whiltelist disabled, and I STILL can't mass assign attributes

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?


Aucun commentaire:

Enregistrer un commentaire