John Doe
John Doe

Reputation: 27

Ruby on rails devise is not permitting all fields

In ruby on rails i use devise for authentication and add the first and last name fields to the database. The new view looks like

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

    <div class="field">
      <%= f.label :first_name %><br />
      <%= f.text_field :first_name, autofocus: true %>
    </div>

    <div class="field">
      <%= f.label :last_name %><br />
      <%= f.text_field :last_name, autofocus: true %>
    </div>

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

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

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

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

The controller looks like this

  private

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

  def account_update_params
    params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :current_password)
  end

When creating the account I get a successful request but I am when I look at the database only the first name attribute show up and not the last name attribute. Form the sql queries generated only the first name attribute gets inserted into the sql statement.

Upvotes: 0

Views: 82

Answers (2)

Mukesh
Mukesh

Reputation: 931

Check Following Code, it will help you

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

  protected

  def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up) do |user_params|
            user_params.permit({ roles: [] }, :email, :password, :password_confirmation)
      end
  end
end  

And for extra help read Devise Strong Parameters topic.

Upvotes: 1

Crashtor
Crashtor

Reputation: 1279

There are a few things you could try:

First do a bang class permitting all variables to be entered in the form and see if last_name gets saved. by declaring .permit! and removing (:email, :password, :password_confirmation, etc...)

If last_name gets saved when banging that class - You should see if entering the correct classes in the form_for tag helps you.

<%= form_for(user, as: user, url: registration_path(user)) do |f| %>

If not, look in your db/schema file and see if you accidently migrated last_name as something other than a string.

Upvotes: 0

Related Questions