The goal is to register a user by name, email. mail and password, and enter the input by entering the name and password (without specifying e-mail).

Install the Devise gem, add the User model: rails g devise User

Added a name to the User model: name (rails generate migration AddNameToUser name: string). migration worked successfully. I add strong parametres to ApplicationController based on my task:

class ApplicationController < ActionController::Base protect_from_forgery with: :exception # настройка для работы девайза при правке профиля юзера before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_in, keys: [:name, :password]) devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :email, :password, :password_confirmation]) end end 

Accordingly, the login form for the user will have only the fields: name and: password (hereinafter referred to as the view itself):

  <h2>Войти</h2> <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <div class="field"> <%= f.label :name, 'имя' %><br /> <%= f.text_field :name, autofocus: true %> </div> <div class="field"> <%= f.label :password, 'твой пароль' %><br /> <%= f.password_field :password, autocomplete: "off" %> </div> <% if devise_mapping.rememberable? -%> <div class="field"> <%= f.check_box :remember_me %> <%= f.label :remember_me, 'хочу чтобы помнили' %> </div> <% end -%> <div class="actions"> <%= f.submit "Вход" %> </div> <% end %> <%= render "devise/shared/links" %> 

Registration of a new user is working fine. And when a user logs in by the username and password, the devise gives "an invalid email or password." If in the view the field: name is replaced in the field: email - then the input is made.

How to reconfigure the gem to log in only by name and password?

    1 answer 1

    Devise defaults to using email as login. To change this behavior, you need to tune it .

    Apparently, you need to do one of two things:

    Or in config/initializers/devise.rb add a line like

     config.authentication_keys = [:name] 

    Or in the model, call the device method call to the form

     devise :database_authenticatable, :authentication_keys => [:name] # т.е. добавить :authentication_keys => [:name] 
    • Everything works, thanks for the reply and the link. - Yar-ua