Extra fields do not save with Devise

2

I'm new to rails and I'm trying to implement Devise with some extra fields. It is saving the email and password correctly, but my first_name and last_name fields are not being saved. It is nil

This is my view of the registration.

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>

<div><%= f.label :first_name %><br />
<%= f.text_field :first_name %></div>

 <div><%= f.label :last_name %><br />
 <%= f.text_field :last_name %></div>

<div><%= f.label :profile_name %><br />
<%= f.text_field :profile_name %></div>

 <div><%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %></div>

<div><%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %></div>

 <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation, autocomplete: "off" %></div>

 <div><%= f.submit "Sign up" %></div>
<% end %>

And this is my User model

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
 # :confirmable, :lockable, :timeoutable and :omniauthable
 devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

end

You are not experiencing any error when saving the user

    
asked by anonymous 04.06.2014 / 02:17

2 answers

3

Devise uses strong parameters , that is, it has to take care of when you want to add new fields to sign_up and sign_in . In this case, I will explain how it should be done for registration ( sign_up ).

If you do not have the additional fields in the database

In this case, you should first insert them in the Users table (or in the analog table if you have not used user as the default). To do this, it is necessary to create a% void%, as follows:

rails generate migration AddFieldsToUsers

This will generate a% void% with the name migrate , in it you must add to the migration all method the fields that you want to be registered (in this case, first_name and last_name). The result should be as follows:

class AddFieldsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
  end
end

Then you should run the following command:

rake db:migrate

This should generate the fields in the AddFieldToUsers table.

Adding Fields to the Record Action

As mentioned, you should use change . To do this, simply add them to your application controller. This will yield a result similar to the following:

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
    devise_parameter_sanitizer.for(:sign_up) << :last_name
  end
end

This should solve your problems.

    
04.06.2014 / 02:57
1

This behavior is expected. Devise requires you to enter additional columns in the application (in addition to adding columns to the database, of course).

To do this in aplication_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
    devise_parameter_sanitizer.for(:sign_up) << :last_name
  end
end

Check out: link

I also recommend add constraints NOT NULL in these two columns.

    
04.06.2014 / 02:48