Search code examples
ruby-on-railsrubyruby-on-rails-4devisesimple-form

Rails 4: Devise not populating users table with correct information


I have a small Rails 4 app that has Devise and Simple Form installed.

My controller is this:

class UserController < ApplicationController
  def create
    User.create(user_params)
  end

  private

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

My form for sign up is as follows:

<h2>Sign up</h2>

<%= 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 :profile_name %><br />
    <%= f.text_field :profile_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 @validatable %>
    <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 %>

However, when I look at the data in the table, it's displaying only the email and password fields. The first and last name, as well as the profile name aren't being stored against the record.

enter image description here

Is there something I've missed here that's preventing all of the fields from being saved on creating a new user? Trying to work out what I've missed here!


Solution

  • Try changing your ApplicationController (the fastest way) according to documentation:

    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 << :last_name << :profile_name
      end
    end
    

    Check according part of documentation.

    Hope that helps!

    PS. 1.

    If you want to customise controller, you should:

    1. Generate one (yours looks like you've created on your own).

      rails generate devise:controllers [scope]

    2. Customise generated controller:

      class Users::SessionsController < Devise::SessionsController # your customisations here # # GET /resource/sign_in # def new # super # end ... end

    Check this part of documentation.

    PS. 2.

    In your code you have a line like:

      params.require(:user).permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation])
    

    it should cause syntax error (I'm surprised if it doesn't). you should remove ] in the end of the list:

      params.require(:user).permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation])