Home>

The login function is created using devise.
I want to be able to log in with my name and password (without re-entry), but I will move to the same page (login screen) without logging in.
When I check the terminal, it seems that the login function is not working properly, but it could not be resolved.

Here is the text of the terminal.

Started POST "/ users/sign_in" for 153.229.14.214 at 2019-11-30 12:34:09 +0000
Cannot render console from 153.229.14.214! Allowed networks: 127.0.0.1, :: 1, 127.0.0.0/127.255.255.255
Processing by Devise :: SessionsController # create as HTML
  Parameters: {"utf8" =>"✓", "authenticity_token" =>"18cq1gPtB2XbEAuEJq5ExY1avQK/AsM4v0MFSeayvjRr82PJhgqdYHSqV8DvoGCZzfOK2NzSaxBXqjfO" RTERfuQ = "," Ruser. , "remember_me" =>"0"},

 "commit" =>"Log in"}
  User Load (0.2ms) SELECT "users". * FROM "users" WHERE "users". "Name" =? AND "users". "Password" =? ORDER BY "users". "Id" ASC LIMIT? [[ "name", "user"],

 ["password", "password"],

 ["LIMIT", 1]]
Completed 401 Unauthorized in 5ms (ActiveRecord: 0.2ms)

Processing by Devise :: SessionsController # new as HTML
  Parameters: {"utf8" =>"✓", "authenticity_token" =>"18cq1gPtB2XbEAuEJq5ExY1avQK/AsM4v0MFSeayvjRr82PJhgqdYHSqV8DvoGCZzfOK2NzSaxBXqjfO" RTERfuQ = "," Ruser. , "remember_me" =>"0"},

 "commit" =>"Log in"}
  Rendering devise/sessions/new.html.erb within layouts/application
  Rendered shared/_header.html.erb (0.9ms)
  Rendered devise/shared/_links.html.erb (0.8ms)
  Rendered shared/_footer.html.erb (0.2ms)
  Rendered devise/sessions/new.html.erb within layouts/application (6.3ms)
Completed 200 OK in 196ms (Views: 45.4ms | ActiveRecord: 0.0ms)

Following sign_up → user page move, the text is as follows.
sign_up can be done by name, email address and password.

Started POST "/ users" for 153.229.14.214 at 2019-11-30 12:29:32 +0000
Cannot render console from 153.229.14.214! Allowed networks: 127.0.0.1, :: 1, 127.0.0.0/127.255.255.255
  ActiveRecord :: SchemaMigration Load (0.1ms) SELECT "schema_migrations". * FROM "schema_migrations"
Processing by Devise :: RegistrationsController # create as HTML
  Parameters: {"utf8" =>"✓", "authenticity_token" =>"1LloI5oUhLvjqp0ZC92ZFkU7lwE24U0XZ1Ep3Kt0CHkIpa5wO3H/kcwUkEOLy78V13XGG8OPcq" user = "= username =" @ gmail.com "," password "=>" [FILTERED] "," password_confirmation "=>" [FILTERED] "},

 "commit" =>"Sign up"}
   (0.1ms) begin transaction
  User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users". "Email" =? LIMIT? [["Email", "email [email protected]"],

 ["LIMIT", 1]]
  SQL (0.7ms) INSERT INTO "users" ("name", "email", "encrypted_password", "created_at", "updated_at") VALUES (?,?,?,?,?) [["Name", " user"],

 ["email", "email [email protected]"],

 ["encrypted_password", "$2a $11 $.dGSv3iaZKCo/WJwJOBoGejJk4D4xdCOCmrCn12fPq3OE4KzBvpE2"],

 ["created_at", "2019-11-30 12: 29: 33.240667"],

 ["updated_at", "2019-11-30 12: 29: 33.240667"]]
   (6.5ms) commit transaction
   (0.0ms) begin transaction
  SQL (0.5ms) UPDATE "users" SET "sign_in_count" =?, "Current_sign_in_at" =?, "Last_sign_in_at" =?, "Current_sign_in_ip" =?, "Last_sign_in_ip" =?, "Updated_at" =? WHERE "users". "id" =? [["sign_in_count", 1],

 ["current_sign_in_at", "2019-11-30 12: 29: 33.267769"],

 ["last_sign_in_at", "2019-11-30 12: 29: 33.267769"],

 ["current_sign_in_ip", "153.229.14.214"],["last_sign_in_ip", "153.229.14.214"],

 ["updated_at", "2019-11-30 12: 29: 33.270255"],

 ["id", 2]]
   (6.4ms) commit transaction
Redirected to https://1fbad43c2e0b482da0849126108a8212.vfs.cloud9.ap-southeast-1.amazonaws.com/users/2
Completed 302 Found in 273ms (ActiveRecord: 15.0ms)

Started GET "/ users/2" for 153.229.14.214 at 2019-11-30 12:29:33 +0000
Cannot render console from 153.229.14.214! Allowed networks: 127.0.0.1, :: 1, 127.0.0.0/127.255.255.255
Processing by UsersController # show as HTML
  Parameters: {"id" =>"2"}
  User Load (0.2ms) SELECT "users". * FROM "users" WHERE "users". "Id" =? ORDER BY "users". "Id" ASC LIMIT? [["Id", 2],

 ["LIMIT", 1]]
  User Load (0.2ms) SELECT "users". * FROM "users" WHERE "users". "Id" =? LIMIT? [["Id", 2],

 ["LIMIT", 1]]
  Rendering users/show.html.erb within layouts/application
  Rendered shared/_header.html.erb (1.0ms)
  Book Load (0.2ms) SELECT "books". * FROM "books" WHERE "books". "User_id" =? [["User_id", 2]]
  Rendered shared/_footer.html.erb (0.3ms)
  Rendered users/show.html.erb within layouts/application (412.0ms)
Completed 200 OK in 575ms (Views: 537.1ms | ActiveRecord: 0.9ms)

・ Completed 401 Unauthorized in 5ms (ActiveRecord: 0.2ms)
・ Completed 200 OK in 196ms (Views: 45.4ms | ActiveRecord: 0.0ms)
・ Completed 302 Found in 273ms (ActiveRecord: 15.0ms)
・ Completed 200 OK in 575ms (Views: 537.1ms | ActiveRecord: 0.9ms)
I think this sentence is a hint.

The following is a list of codes.

20191008135158_devise_create_users.rb
# frozen_string_literal: true
class DeviseCreateUsers</pre>
<pre><code>user.rb
class User  [: name,: password]
  has_many: books, dependent:: destroy
  attachment: profile_image
end
application_controller.rb
class ApplicationController</pre>
<pre><code>routes.rb
Rails.application.routes.draw do
  devise_for: users
  get root 'top # top'
  get '/ home/about', to: 'top # home'
  resources: users
  resources: books
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
  • Answer # 1

    It seemed that the settings when logging in with the name were wrong.

    Fix or add the following

    routes.rb
    devise_for: users, controllers: {
      sessions: 'users/sessions',
      registrations: 'users/registrations'
      }
    controllers/users/registrations_controller.rb
    class Users :: RegistrationsController
    controllers/users/sessions_controller.rb
    class Users :: SessionsController
    config/initializers/devise.rb
    # config.authentication_keys = [: email]
       config.case_insensitive_keys = [: email]
       config.strip_whitespace_keys = [: email]
          ↓ Rewrite
       config.authentication_keys = [: name]
       config.case_insensitive_keys = [: name]
       config.strip_whitespace_keys = [: name]

    Delete the following

    application_controller.rb
      before_action: authenticate_user!
      def after_sign_up_path_for (resource)
        user_path (current_user)
      end
      def after_sign_in_path_for (resource)
        user_path (current_user)
      end
    user.rb
    : authentication_keys =>[: name,: password]