Rails 4.0 & Devise - Strong Parameters error

2019-09-06 19:58发布

问题:

Newbie to rails here, so bear with me.

New app on Rails 4 with ruby 2.0, I installed Devise and followed the instructions(default root, etc). Devise readme on github says it should be compatible with rails4 but

  • db:migrate failed unless I commented out attr_accessible line in User.rb
  • After commenting that out, I get "ActiveModel::ForbiddenAttributesError in Devise::RegistrationsController#create" error in trying to create a user.

I see some stack overflow questions like this, but a lot of the answers jump straight into some complex talk. I get I need to specify permitted attributes for mass assignment, but how? And where? And which attributes need to be permitted, all of them? Only those that I expect to be changed/created at the same time?

Judging by the error would I create a registrations_controller.rb that inherits from Devise::registrationsController ? What do I specify in that?

Any step by step, newbie friendly answers are much appreciated. I've exhausted myself trying different code from answers here and various sites from google searches.

回答1:

Welcome to stackoverflow!

The problem is that the functinality of attr_accessible changed in rails 4.0

2 possibilities to get it running

1 Update Devise that it can handle Rails 4.0

Add this line to your application's Gemfile:

gem 'devise', '3.0.0.rc' 

And then execute:

$ bundle

2 Add the old functionality of attr_accessible again to rails 4.0

Try to use attr_accessible and dont comment this out.

Add this line to your application's Gemfile:

gem 'protected_attributes'

And then execute:

$ bundle


回答2:

Adding below gems and doing a bundle install worked for me

source 'https://rubygems.org'

gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.3.2.0'
gem 'devise', '3.0.0.rc' 
gem 'protected_attributes'
gem 'sqlite3'