Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Username to devise rails 4

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!

like image 261
infinity Avatar asked Jul 08 '13 23:07

infinity


1 Answers

You're only permitting the username to be allowed on the sign in method, but I'm assuming when you create a new user, it's on the sign up method. So try this:

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

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

like image 79
1andsock Avatar answered Nov 15 '22 22:11

1andsock