前提・実現したいこと
プログラミング初心者です。練習としてTwitterのようなサービスを作っているのですが、ログインができなくて困っています。
発生している問題・エラーメッセージ
ログインしようとすると”メールアドレスまたはパスワードが間違っています。”というメッセージが出てしまってログインできません。
メールアドレスもパスワードも間違っていないのですがエラーが出てしまいます。
該当のソースコード
class UsersController < ApplicationController protect_from_forgery except: :logout before_action :authenticate_user, {only: [:index, :show, :edit, :update]} before_action :forbid_login_user, {only: [:new, :create, :login_form, :login]} before_action :ensure_correct_user, {only: [:edit, :update]} def create @user = User.new(user_params) @user.image_name = "default_user.jpg" @user.profile = "こんにちは!" if @user.save session[:user_id] = @user.id flash[:notice] = "ユーザー登録が完了しました" redirect_to("/users/#{@user.id}") else render("users/new") end end def login_form end def login @user = User.find_by(email: params[:email]) if @user && @user.authenticate(params[:password]) session[:user_id] = @user.id flash[:notice] = "ログインしました" redirect_to("/posts/index") else @error_message = "メールアドレスまたはパスワードが間違っています" @email = params[:email] @password = params[:password] render("users/login_form") end end def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation, :image_name, :profile) end
#login_form.html.erb <div class="main users-new"> <div class="container"> <div class="form-heading">ログイン</div> <div class="form users-form"> <div class="form-body"> <% if @error_message %> <div class="form-error"> <%= @error_message %> </div> <% end %> <%= form_tag("/login") do %> <p>メールアドレス</p> <input name="email" value="<%= @email %>"> <p>パスワード</p> <input type="password" name="password" value="<%= @password %>"> <input type="submit" value="ログイン"> <% end %> </div> </div> </div> </div>
get "login" => "users#login_form" post "login" => "users#login" post "logout" => "users#logout"
user = User.create(name: "taro", email: "taro@example.com", password: "foo", password_confirmation: "foo") (0.1ms) begin transaction User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = ? LIMIT ? [["email", "taro@example.com"], ["LIMIT", 1]] User Create (1.6ms) INSERT INTO "users" ("name", "email", "created_at", "updated_at", "password_digest") VALUES (?, ?, ?, ?, ?) [["name", "taro"], ["email", "taro@example.com"], ["created_at", "2019-04-28 21:47:51.443175"], ["updated_at", "2019-04-28 21:47:51.443175"], ["password_digest", "$2a$10$A6kLyqa5sEYJhYGKNL1aUOX3kaYAReqq4HYzERSEuO55.TPn7r15S"]] (7.8ms) commit transaction => #<User:0x005608d9b11718 id: 1, name: "taro", email: "taro@example.com", created_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00, updated_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00, image_name: nil, password_digest: "$2a$10$A6kLyqa5sEYJhYGKNL1aUOX3kaYAReqq4HYzERSEuO55.TPn7r15S", profile: nil> [2] pry(main)> user.authenticate("foo") => #<User:0x005608d9b11718 id: 1, name: "taro", email: "taro@example.com", created_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00, updated_at: Sun, 28 Apr 2019 21:47:51 UTC +00:00, image_name: nil, password_digest: "$2a$10$A6kLyqa5sEYJhYGKNL1aUOX3kaYAReqq4HYzERSEuO55.TPn7r15S", profile: nil>
class User < ApplicationRecord has_secure_password validations: true mount_uploader :image, ImageUploader validates :name,{presence: true} validates :email,{presence: true, uniqueness: true} has_many :posts, dependent: :destroy def posts return Post.where(user_id: self.id) end end
試したこと
新規登録した際の情報をrails consoleで確認してみるとしっかり入っていたのでDBに保存はされているとは思うのですが…
一致しないということはログインする際に必要な情報がDBにないということだと思うのですが解決方法がわからなくて困っています!
よろしくお願いします!!
回答1件
あなたの回答
tips
プレビュー