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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With