Have the surname and given name entered separately and combine them before saving

It is a memorandum. New registration with Devise Have the first and last name form filled in separately and combine them before saving

erb:registrations/new.html.erb


  <%= f.label :firstName, "Surname" %>
  <%= f.text_field :firstName, autofocus: true, required: true, class: 'form-control' %>
  <%= f.label :lastName, "Name" %>
  <%= f.text_field :lastName, autofocus: true, required: true, class: 'form-control' %>

controllers/application_controller.rb


class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?
  
  protected
  #Last name and first name attributes with strong parameter(firstName and lastName)Permit
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:firstName, :lastName])
  end
end

models/user.rb


  #Combine last name and first name before saving to DB
  before_create :create_name
  def create_name
    self.name = "#{firstName} #{lastName}"
  end

Recommended Posts

Have the surname and given name entered separately and combine them before saving
Have them enter their name and email address before deleting the user account