Extra Fields Don’t Save With Devise

Asked

Viewed 841 times

2

I’m new to Rails and I’m trying to implement Devise with a few extra fields. It is saving the email and password correctly, but my fields first_name and last_nameare not being saved. Are nil

This is my view of 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 registering any error when saving the user

  • It’s Rails 3 or Rails 4?

  • Sorry, Rails 4.1.1

  • You already have the columns first_name and last_name in the database?

2 answers

3


The Devise uses Strong Parameters, that is, it has to take care when you want to add new fields to the sign_up and sign_in. In this case, I will explain how it should be done for the record (sign_up).

If you do not have the additional fields in the database

In this case, you should first insert them in the table Users (or in the analogous table if you have not used user by default). This requires creating a migrate empty, as follows:

rails generate migration AddFieldsToUsers

This will generate a migration empty with the name AddFieldToUsers, in it you must add to the method change all the fields you want to be recorded (in this case first_name and last_name). The result should be the following:

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

Then the following command must be executed:

rake db:migrate

This should generate the fields in the table Users.

Adding fields to the record action

As mentioned above, the strong parameters. To do this, simply add them to your application controller. This will generate 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

That should solve your problems.

  • 1

    When you have many elements you can pass a block devise_parameter_sanitizer.for(:sign_up) from |u| u.Permit(:first_name, :last_name) end

1

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

So do it like this on 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: https://github.com/plataformatec/devise

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.