7

I have a rails 4 application using devise. I'm trying to allow users to have a username associated with them.

I've added a username (string) column to the Users table, and had my Application controller look like this:

class ApplicationController < ActionController::Base

    before_filter :configure_permitted_parameters, if: :devise_controller?
    protect_from_forgery with: :exception
    protected
    def configure_permitted_parameters
       devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email) }
    end

end

and I've also added a field for the username on the users/sign_up page.

But I get this error:

undefined local variable or method `devise_parameter_sanitizer' for #<Devise::RegistrationsController:0x00000101378a28>

So basically my question is why is this error appearing, or how else can I get a user to get a username?

Thanks for all help!

4

1 回答 1

16

您只允许在登录方法中允许使用用户名,但我假设当您创建新用户时,它在注册方法中。所以试试这个:

def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

来源:https ://github.com/plataformatec/devise#strong-parameters

于 2013-07-09T17:46:22.833 回答