Reputation: 699
I am porting an old Rails 3 application to Rails 4.0.15 and everything seems to be going fine except that whenever I try to create a record (even in Rails console) I am getting the above error (undefined method '[]' for nil:NilClass).
I am over-simplifying things a little here but what I did was to create a new app (rails new xxxx
) then copy the app/
and db/
tree over from the old. I used the same db. routes.rb and Gemfile were adjusted manually.
One of the models is Contact:
class Contact < ActiveRecord::Base
attr_accessible :name, :email, :phone, :address
end
In rails console
I get the following when I type Contact.create
:
Loading development environment (Rails 4.1.5)
irb(main):001:0> Contact.create
NoMethodError: undefined method `[]' for nil:NilClass
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/read.rb:113:in `block in read_attribute'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/read.rb:111:in `fetch'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/read.rb:111:in `read_attribute'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/attribute_methods/primary_key.rb:19:in `id'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:344:in `remember_transaction_record_state'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:282:in `rollback_active_record_state!'
from /usr/share/gems/gems/activerecord-4.1.5/lib/active_record/transactions.rb:267:in `save'
from /home/wucolin/.gem/ruby/gems/protected_attributes-1.0.3/lib/active_record/mass_assignment_security/persistence.rb:46:in `create'
from (irb):1
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/console.rb:90:in `start'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/console.rb:9:in `start'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/share/gems/gems/railties-4.1.5/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:8:in `require'
from bin/rails:8:in `<main>'
Best case scenario: I'm hoping someone would see this and go "Yeah, that's easy to fix. All you do is..."
However, I would be quite happy if someone could point me in the right direction or where to start.
Thanks all for you time.
Upvotes: 1
Views: 690
Reputation: 176562
The "attr_accessible" feature is gone in Rails 4. I suggest to not use the gem, there have been many reports of issues.
Rather than pushing "protected" into Rails 4, install the strong_parameters
gem and backport the strong parameters feature in Rails 3.
It is definitely a more appropriate approach.
Upvotes: 1