Home Add username to Devise
Reply: 2

Add username to Devise

Pagaley 12
1#
Pagaley 12 Published in 2018-01-13 02:14:40Z

I ran "rails g migration add_username_to_users username:string" and "rake db:migrate" and added this to my application_controller

before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
   devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
   devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
 end

but I get this error when I try to load a devise view NoMethodError in Devise::RegistrationsController#new undefined method `for' for # Did you mean? fork Extracted source (around line #7)

Line 7 is: devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }

UPDATE

now I am receiving a different error

NoMethodError in Devise::Registrations#new

 <%= f.username_field :username, :autofocus => true, autocomplete: "username" %>

Update 2 Now the sign up page loads but when I press the sign up button I get this error LoadError in Devise::RegistrationsController#create cannot load such file -- bcrypt_ext

 load_dependency(file) { result = super }
Daniel
2#
Daniel Reply to 2018-01-13 02:19:56Z

Your usage is wrong, this the right waydevise_parameter_sanitizer.permit(:sign_up, keys: [:subscribe_newsletter]). This the docs https://github.com/plataformatec/devise#strong-parameters

fool-dev
3#
fool-dev Reply to 2018-01-13 03:48:10Z

Try to the following

On application_controller.rb

before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :username, :password, :password_confirmation])

    devise_parameter_sanitizer.permit(:sign_in, keys: [:login, :username, :email, :password, :remember_me])

  devise_parameter_sanitizer.permit(:account_update, keys: [:username, :email, :password, :password_confirmation, :current_password])
end

This now permit your extra column keys like username for more click here

Now, devise by default use bcrypt for password encryption, you need to install bcrypt for password encryption option like below

gem install bcrypt
OR
gem install bcrypt --platform=ruby

Hope to help

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.331069 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO