Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding first name and last name to devise sign up form

I'm using Rails 4 and I'm trying to add first name and last name fields to the sign up process. I'm using Devise to handle authentication.

I've added first_name and last_name columns to my user model through a migration. I also added the following to my application_controller.rb file:

before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :remember_me) }
  devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password) }
end

However when I sign up, the first_name and last_name field can be left blank and even when they are populated the values are not saved to the user object.

How can I:

  1. Force those fields to be required on sign up? I tried adding 'validates_presence_of :first_name, :last_name, :email' to the user model but had to remove it since the form for some reason always thinks the first_name and last_name fields are blank.
  2. Update devise create action so that it saves the values passed in through the devise/registrations/new.html.erb file?
  3. I have a Person model that i want to associate with this user (associations are created successfully and i have forms that edit both person and user fields but not sure how to build that into the devise sign up process.

Any help would be appreciated.

like image 561
TaimoorQ Avatar asked Oct 20 '22 18:10

TaimoorQ


1 Answers

This is covered in the devise wiki

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-sign-in-with-something-other-than-their-email-address

like image 187
Donovan Avatar answered Oct 29 '22 17:10

Donovan