Home > Blockchain >  Ruby On Rails Devise Register and Login with Username instead of Email
Ruby On Rails Devise Register and Login with Username instead of Email

Time:10-04

I'm trying add a username field to my register using devise views/devise/registrations/new/html.erb

<h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= render "devise/shared/error_messages", resource: resource %>

  <div class="field">
    <%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %>
  </div>
  
  <div class="field">
    <%= f.label :username %><br />
    <%= f.text_field :username, autofocus: true, autocomplete: "email" %>
  </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: "new-password" %>
  </div>

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

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

<%= render "devise/shared/links" %>

I got an error: "undefined method `username' for #Usermanagement:0x00007f3db801a8e0" What did I do wrong? I'm new with ROR so please help me to figure out. Thank you!

CodePudding user response:

in addition to the comment from @hazg, you might find the below link from the devise wiki helpful.

This is a walkthrough on how to add the username to your devise model:

https://github.com/heartcombo/devise/wiki/How-To:-Allow-users-to-sign-in-with-something-other-than-their-email-address

CodePudding user response:

Assuming that you have named your Devise Model a User

  • Add username field to Devise User Model
$ rails generate migration add_username_to_users name:string
  • Migration file should look like this
class AddUsernameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :username, :string
  end
end
  • Migrate the Database
$ rails db:migrate
  • Generate Devise views
$ rails generate devise:views -v registrations
  • Update sign up & edit profile form
# views/devise/registrations/new.html.erb
# views/devise/registrations/edit.html.erb

  <div class="field">
    <%= f.label :username %><br />
    <%= f.text_field :username, autofocus: true %>
  </div>
  • Override the devise controller
  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_filter :configure_permitted_parameters, if: :devise_controller?

    protected

        def configure_permitted_parameters
            devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :email, :password])
            devise_parameter_sanitizer.permit(:account_update, keys: [:username, :email, :password, :current_password])
        end
  end
  • Add model validations
class User < ApplicationRecord
  validates_presence_of :username
  # ...
end
  • Related