Setting up development using strong parameters

I am using Rails 4.0.0 and Devise 3.0.2 and trying to configure Devise with strong parameters by following this instruction in Devise README.

I wrote code like this in application_controller.rb

 class ApplicationController < ActionController::Base before_filter :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) << :nick end end 

Then I visited http://localhost:3000/users/sign_up . I got a NoMethodError in Devise::RegistrationsController#new which says:

undefined method <<' for {}:ActionController::Parameters

and points to the exact line in which I wrote devise_parameter_sanitizer.for(:sign_up) << :nick

Is there something I did wrong? Thank you for your help.

+6
source share
3 answers

As Jose Valim said , this is Devise 3.1.0.rc, so it does not work. You should use other syntaxes that are in README.

+4
source

Try:

  class ApplicationController < ActionController::Base ... before_filter :configure_permitted_parameters, if: :devise_controller? ... def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :nick) } end 

This works for me !: D

+5
source

The problem exactly matches your problem: # 2574: devise_parameter_sanitizer.for (: sign_up) <<: something is causing an error .

In fact, the method of adding custom fields to strong parameters is a new feature included with Devise 3.1.

Since the current version in Rubygems.org is 3.0.3 , you cannot use this method in your rails project. You will have to override the default values:

 devise_parameter_sanitizer.for(:sign_up) do |u| u.permit :email, :password, :password_confirmation, :first_name, :last_name end 


But if you really need to, you can edit your Gemfile and replace this line

 gem 'devise', '3.0.3' 

with this:

 gem 'devise', github: 'plataformatec/devise', branch: 'master' 

Then you can easily add your custom fields:

 # Single field devise_parameter_sanitizer.for(:account_update) << :first_name # Multiple fields at a time devise_parameter_sanitizer.for(:account_update) << [:first_name, :last_name] 

But it will be warned , currently it is Release Candidate : 3.1.0 RC1

+4
source

Source: https://habr.com/ru/post/951924/


All Articles