Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an extra field in the registration form generated by devise?

I'm learning Ruby, OOP and Rails.

My app uses the Devise gem for registering users.

It only offers the fields email, password, and password confirmation, but I need an extra field, phone number.

What is the correct way to add it? (Not just in the html file)

like image 619
Gutimore Avatar asked Jun 17 '17 03:06

Gutimore


1 Answers

1) You need to add the extra fields to your User Model...

rails g migration AddPhoneNumberToUser phone_number:string

2) Configure Strong Parameters in ApplicationController

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

  protected

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

NOTE: if you already have custom controllers, you just need to uncomment (overhide) RegistrationsController#sign_up_params method

3) Generate devise views (if you didn't it yet)

rails generate devise:views

4) Add extra fields to form in app/views/devise/registrations/new.html.erb

like image 133
Thiago Ururay Avatar answered Nov 10 '22 14:11

Thiago Ururay