Damir Nurgaliev
Damir Nurgaliev

Reputation: 351

How to add extra field in devise auth rails

I'm doing auth with devise. I need also extra field name and type of user. Here is migration:

class AddColoumnToUsers < ActiveRecord::Migration
  def change
        add_column :users, :name, :string
        add_column :users, :type, :string
  end
end

And then registration field

  <%=simple_form_for(resource, as: resource_name, url: registration_path(resource_name), :html => {:class => 'form-horizontal' }) do |m| %>

              <%= m.input :name%>
              <%=m.input :email %>
              <%=m.input :password %>
              <%=m.input :password_confirmation %>

          <%=m.button :submit %>


    <% end %>

All works fine, but after reg if i tried to welcome user by name got empty or error.

I do like this in profile

<%= current_user.name %>

The error is no method error or if i add @ just return empty field. Can anyone help?

Upvotes: 0

Views: 1107

Answers (2)

Ankita Tandel
Ankita Tandel

Reputation: 202

Add in your application controller

before_action :configure_permitted_parameters, if: :devise_controller?



protected

  def configure_permitted_parameters
    registration_params = [ :name, :type, :email, :password, :password_confirmation ]
    devise_parameter_sanitizer.for(:sign_up) {
      |u| u.permit(registration_params)
    }
  end

Upvotes: 0

Sudarshan Dhokale
Sudarshan Dhokale

Reputation: 206

You have to override Registration controller of devise

First create one controller file as app/contollers/registrations_controller.rb And write below code in that file

    class RegistrationsController < Devise::RegistrationsController

      private

      def sign_up_params
        params.require(:user).permit!
      end

      def account_update_params
       params.require(:user).permit!
      end
    end

OR instead of permit! you can add parameter like this

params.require(:user).permit(:name, :type, .......'other user paramete')

And final change in your routes file like below

devise_for :users, controllers: { registrations: 'registrations' }

Upvotes: 1

Related Questions