My devise User model custom field username always nil. I have tried all the other solutions on stack overflow

thedreamsaver

My application controller file

  class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up)  {|u| u.permit(:username, :email, :password, :password_confirmation, :remember_me)}
    devise_parameter_sanitizer.permit(:sign_in)  {|u| u.permit(:username, :email, :password, :password_confirmation, :remember_me)}
    devise_parameter_sanitizer.permit(:account_update) {|u| u.permit(:email, :password, :password_confirmation, :remember_me)}
  end
end

My routes.rb file

Rails.application.routes.draw do
  devise_for :users

  root 'pages#index'

  get 'home' => 'pages#home'

  get 'profile' => 'pages#profile'

  get 'explore' => 'pages#explore'


end

My migration files

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

My add username column migration

class AddUsernameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :username, :string
    add_index :users, :username, unique: true
  end
end

My views/devise/registrations/new.html.erb

<%= bootstrap_devise_error_messages! %>
<div class="panel panel-default devise-bs">
  <div class="panel-heading">
    <h4><%= t('.sign_up', default: 'Sign up') %></h4>
  </div>
  <div class="panel-body">
    <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { role: 'form' }) do |f| %>
      <div class="form-group">
        <%= f.label :username %>
        <%= f.text_field :email, autofocus: true, class: 'form-control' %>
      </div>
      <div class="form-group">
        <%= f.label :email %>
        <%= f.email_field :email, class: 'form-control' %>
      </div>
      <div class="form-group">
        <%= f.label :password %>
        <%= f.password_field :password, class: 'form-control' %>
      </div>
      <div class="form-group">
        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation, class: 'form-control' %>
      </div>
      <%= f.submit t('.sign_up', default: 'Sign up'), class: 'btn btn-primary' %>
    <% end %>
  </div>
</div>
<%= render 'devise/shared/links' %>

So even after using strong parameters when the usernames being added through browser are not reflected back in the database.

It gives this result

irb(main):001:0> u = User.all
  User Load (20.0ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 1, email: "[email protected]", created_at: "2017-07-05 19:42:19", updated_at: "2017-07-05 21:11:38", username: nil>, #<User id: 2, email: "[email protected]", created_at: "2017-07-06 08:45:18", updated_at: "2017-07-06 08:45:18", username: nil>, #<User id: 3, email: "[email protected]", created_at: "2017-07-06 08:56:15", updated_at: "2017-07-06 08:56:15", username: nil>, #<User id: 4, email: "[email protected]", created_at: "2017-07-06 09:23:55", updated_at: "2017-07-06 09:23:55", username: nil>]>

Any help will be appreciated. Also tell me if you need to see any other files.

Pavan

My devise User model custom field username always nil

Because you don't have it in the form. Change

<div class="form-group">
  <%= f.label :username %>
  <%= f.text_field :email, autofocus: true, class: 'form-control' %>
</div>

to this

<div class="form-group">
  <%= f.label :username %>
  <%= f.text_field :username, autofocus: true, class: 'form-control' %>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to extract href link from this given code using bettersoup. i have tried other results on stack overflow

Data not moving view to controller, I have tried with all possible solutions

FieldDoesNotExist error when using Dj-Rest-Auth registration with a custom user model that does not have a username field

Login user for a custom user model with email as username field

Updating custom field for devise user model in Rails 4

CSS overflow-x:hidden; not working on mobile, tried all the solutions I could find here

Jquery Ajax POST is not getting dynamically added inputs inputs have names and I have tried other solutions

My User Model changes 'email' to username in admin panel when i update any field

i am having an error "out of memory error" i have tried all solutions but i am stuck with that error

How can I add information to custom user field (Devise)

NoSuchElementException in Stack trace, tried some other solutions in the community still not helpful

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017. I have tried all solutions given in the StackOverflow, but its not working

How do I add username of logged in user to model field in django

I have tried all solutions I have found yet I can't find a solution - Pylance(missingImports) for vs code

Devise: I added a username field, but it's not working

Django custom User model throwing SystemCheckError - The field 'username' clashes with the name 'username'

I want to return a user with a list of all the people they have referred by there username

Chrome process eating all my bandwidth, tried all possible solutions

why am i not getting css and js files in my dynamic web project i tried many ways as mentioned on stack overflow but its not working

org.hibernate.LazyInitializationException: All possible solutions have been tried :(

I want my input field to always have two decimal places

How to change the field of username to user_name in to django custom user model?

Use devise for a model other than user

Why does my Registrations#Update devise action not update all attributes in my `User` model?

my application is not showing up in the emulator (tried all the solutions on stackoverflow)

WARNING: No mapping for GET /StudentCurd/ (HTTP Status 404 – Not Found) I have tried all the solutions but not able to sole this, so asking again:

Editing custom fields in Devise User model

Unable to add custom fields to my Devise user model in Rails 4 with strong parameters

Multiple USERNAME_FIELD in django user model