Rails 5, Undefined method `for' for #<Devise on line devise_parameter_sanitizer.for
Asked Answered
M

6

102

I am working with Rails 5

I aded new field username in model User.

class Users::RegistrationsController < Devise::RegistrationsController
  before_action :configure_permitted_parameters

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up).push(:username)  
  end
end

During registration is displayed error: undefined method `for' for # Did you mean? fork

Trace:

NoMethodError (undefined method `for' for # Did you mean? fork):

app/controllers/users/registrations_controller.rb:7:in `configure_permitted_parameters'
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (5.0ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.9ms)
  Rendering /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.2ms)
  Rendered /usr/local/rvm/gems/ruby-2.3.0/gems/actionpack-5.0.0.rc1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (118.1ms)

Who can help? How solve this problem?

Malnourished answered 20/5, 2016 at 8:40 Comment(1)
It happens on Rails 4.2 also.Thimbleweed
L
187

According to the documentation:

The Parameter Sanitaizer API has changed for Devise 4

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
  end
end
Ludovika answered 20/5, 2016 at 8:54 Comment(4)
for some reason this works but when i restart rails it stops working againMeathead
@Meathead Clear your bin? Oops, its October. How did you fix this?Aldred
stop + restart spring, pehapsElisabeth
Thanks! For those not wanting to update their code, you can just adjust your Gemfile accordingly - gem 'devise', '~> 3' sorted it for me.Honesty
K
34

If you just change the .for to .permit it works as well. For example:

devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation, :username) }

It works in both Rails 4.2.x and Rails 5.0.x

Krys answered 11/8, 2016 at 17:15 Comment(0)
S
2

Don't forget devise_parameter_sanitizer.permit(:account_update, keys: [:username])

Sorensen answered 12/7, 2016 at 23:45 Comment(1)
He did not forget, he needs to switch to new syntax.Thimbleweed
B
0

I think you missed account_update in your controller's configure_permitted_parameters method, you need to follow the devise pattern. Devise has a an account update page. You can find this in views/devise/registrations/edit.html.erb, and your code is also not going to work in the sign_up page, here you specified sign_up page

To update your user table, the minute you submit an update in your users/edit, or if you are submitting a username in the sign_up page you need to follow this devise pattern, to update the database User table. Even if you added a new column to the user table, you would have to add it to the configure_permitted_parameters method. In your case it's username, but you missed account_update as well. You're basically saying that you want to update the username or add the string to username field without following the Devise pattern. Any field you add to the User table should follow this Devise pattern. Also you can specify which page is permitted to update this username. In my example below, i'm using the devise update page. So like I said, even if you added a custom field name to Users table you need to follow this pattern. If you have another page where you need to add username, you would just do the same thing.

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username])
  end
end

Next make sure in your user.rb you have validate username in your User model.

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  validates :username, presence: true
end
Bercy answered 26/3, 2019 at 15:54 Comment(0)
P
0

To update your user table, the minute you submit an update in your users/edit, or if you are submitting a username in the sign_up page you need to follow this devise pattern, to update the database User table. Even if you added a new column to the user table, you would have to add it to the configure_permitted_parameters method. In your case it's username, but you missed account_update as well. You're basically saying that you want to update the username or add the string to username field without following the Devise pattern. Any field you add to the User table should follow this Devise pattern. Also you can specify which page is permitted to update this username. In my example below, i'm using the devise update page. So like I said, even if you added

Postaxial answered 31/1, 2022 at 3:56 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Eakins
B
-1
class ApplicationController < ActionController::Base

  before_action :configure_permitted_paramters, if: :devise_controller?

  protected
    def configure_permitted_paramters

        devise_parameter_sanitizer.permit(:sign_up, keys: [:fullname])

        devise_parameter_sanitizer.permit(:account_update, keys: [:fullname, 
        :phone_number, :description, :email, :password])

    end

end
Bercy answered 12/6, 2017 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.