How to add a gender column to user model under devise on rails?

In the application_controller.rb

before_action :configure_devise_permitted_parameters, if: :devise_controller?

protected

def configure_devise_permitted_parameters
  registration_params = [:name, :sex, :description, :email, :password, :password_confirmation]

  if params[:action] == 'update'
    devise_parameter_sanitizer.for(:account_update) { 
      |u| u.permit(registration_params << :current_password)
    }
  elsif params[:action] == 'create'
    devise_parameter_sanitizer.for(:sign_up) { 
      |u| u.permit(registration_params) 
    }
  end
end

Generage a migration to add some custom fields to users table:

class AddNameSexDescriptionToUsers < ActiveRecord::Migration
  def change
    add_column :users, :name, :string
    add_column :users, :sex, :integer, default: 0
    add_column :users, :description, :text
  end
end

Add enum to sex in the user model:

enum sex: [:unknown, :male, :female]

Read data in the devise’s registrations/edit.html.erb view:

<%= f.select :sex, options_for_select(User.sexes.collect { |s| [s[0].humanize, s[0]] }, selected: current_user.sex), {}, class: "form-control" %>

Another way, add a helper method in the user model:

def self.sexes_for_select
  sexes.keys.map{ |x| [x.humanize, x] }
end

Then the view(registrations/edit.html.erb) will be:

<%= f.select :sex, User.sexes_for_select, {}, class: "form-control" %>

Useful links