前提・実現したいこと
Railsチュートリアルを模倣しながらオリジナルアプリを作成しています。
作成段階でチュートリアル通りにログイン機能をつけてユーザー登録をしてからログインするつもりなのですが、どうしてもログイン出来ません。
発生している問題・エラーメッセージ
!メールアドレスに「@」を挿入してください。
123456に「@」がありませんと表示されます。
該当のソースコード
new.html.erb
<h1>ログイン</h1> <div class="row"> <div class="col-md-6 col-md-offset-3"> <%= form_for(:session,url:login_path) do |f| %> <%= f.label :email %> <%= f.email_field :email,class:'form-control'%> <%= f.label :password %> <%= f.email_field :password,class:'form-control'%> <%= f.submit "Log in", class:"btn btn-primary"%> <% end %> <p>New user? <%= link_to "Sign up now!", signup_path%></p> </div> </div>
sessions_controller.rb
class SessionsController < ApplicationController def new end def create user = User.find_by(email:params[:session][:email].downcase) if user && user.authenticate(params[:session][:password]) # ユーザーログイン後にユーザー情報のページにリダイレクトする log_in user redirect_to user else # エラーメッセージを作成する flash.now[:danger] = 'Invalid email/password combination' render 'new' end end def destroy log_out redirect_to root_url end end
sessions_helper.rb
module SessionsHelper # 渡されたユーザーでログインする def log_in(user) session[:user_id] = user.id end # 現在ログイン中のユーザーを返す (いる場合) def current_user if session[:user_id] @current_user ||= User.find_by(id:session[:user_id]) end end # ユーザーがログインしていればtrue、その他ならfalseを返す def logged_in? !current_user.nil? end # 現在のユーザーをログアウトする def log_out session.delete(:user_id) @current_user = nil end end
user.rb
class User < ApplicationRecord before_save{self.email=email.downcase} validates :name,presence:true,length:{maximum:30} VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i validates :email,presence:true,length:{maximum:255}, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password,presence:true,length:{minimum:6} # 渡された文字列のハッシュ値を返す def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end end
試したこと
railsチュートリアルのコードを見直して自分のコードと照らし合わせた。
是非何かわかれば教授願います。
お願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/26 04:59