Adding Fields to Devise Sign Up Using Rails 4

How do i add a custom field to a devise registration in Rails 4?

You can also use following approach to add custom field in devise user table.

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.

before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:firstname, :lastname, :email, :password, :password_confirmation) } # The :firstname and :lastname are my custom fields.
end

protect_from_forgery with: :exception
end

How to add an extra field in the registration form generated by devise?

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

Adding Custom Fields to Devise Registration - Ruby on Rails 4 & Devise 3

May be problem with Strong Parameters. Try to add following code to application_controller.rb.

class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :first_name
end
end

For more information: https://github.com/plataformatec/devise#strong-parameters

Add Custom Field/Column to Devise with Rails 4

Once your model has its full_name attribute, you will have to configure permitted parameters for the #sign_up and #account_update Devise actions.

class ApplicationController < ActionController::Base
before_action :configure_devise_permitted_parameters, if: :devise_controller?

protected

def configure_devise_permitted_parameters
registration_params = [:full_name, :email, :password, :password_confirmation]

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

end

Adding extra registration fields with Devise

OK, so what I did was just override the Devise registration controller, update routes.rb as per the devise docs to reflect this, copied and pasted the Devise code for registrations#create as is, and change the getting params part to use my own strong parameters method, and that was that.

class RegistrationsController < Devise::RegistrationsController

def create
build_resource(registration_params)

if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
respond_with resource, :location => after_sign_up_path_for(resource)
end
else
clean_up_passwords
respond_with resource
end
end

private

def registration_params
params.require(:user).permit(:email, :title_id, :first_name, :last_name,
:province_id, :password, :password_confirmation)
end

end

Adding Fields To Devise Sign Up Using Rails 4

With "f.something" you can just define the type of the field in the form. Try something like

<%= f.text_field :lastName %>

This should work.

You can read about the available types of fields here: http://apidock.com/rails/ActionView/Helpers/FormHelper/form_for

How do I add additional fields to the Devise 'sign up' page?

Just add columns to the user and then add additional features in the views.

script/generate migration add_user_type_to_user

Then in your migration rake file:

add_column :users, :user_type, :string

and then rake db:migrate. Next you can add a drop down to your view and you can get those through rails generate devise:views:

<%= f.select :user, :user_type options_for_select(['admin', 'no status'])%>

Saving custom fields in devise User model in rails 4

In Rails4 we have strong parameters so please

Add following line to your application_controller.rb

before_filter :configure_devise_params, if: :devise_controller?
def configure_devise_params
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:first_name, :last_name, :gender, :email, :password, :password_confirmation)
end
end

How to add a custom field to devise in Rails 4 and customise the registration controller?

Is there a username column in the users table in the db? I would guess not, given that you've declared attr_accessor :username -- an ActiveRecord model should automatically create accessors for its database fields. So my guess is that username isn't saved in the database because there isn't a username field in the database. Did you do a migration when you added the User model?

If there is a username field, try removing attr_accessor :username. You're probably overwriting the built-in ActiveRecord getter/setter methods.



Related Topics



Leave a reply



Submit