Gutimore
Gutimore

Reputation: 385

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)

Upvotes: 5

Views: 2568

Answers (1)

Thiago Ururay
Thiago Ururay

Reputation: 462

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

Upvotes: 10

Related Questions