前提・実現したいこと
ここに質問の内容を詳しく書いてください。
初めての質問です。
現在、ruby on railsを勉強している初心者です。
画像投稿サイトのサインインの認証チェックを実行しようと思い以下を書いたところerrorに原因が分からず困っています。
よろしければご教授いただければ幸いです。
エラーメッセージ
Routing Error
undefined method `user_signed_in?' for UsersHelper:Module
該当のソースコード
users_controller.rb class UsersController < ApplicationController before_action :authorize, except: [:sign_up, :sign_up_process, :sign_in, :sign_in_process] before_action :redirect_to_top_if_signed_in, only: [:sign_up, :sign_in] def top end # ユーザー登録ページ def sign_up @user = User.new render layout: "application_not_login" end # サインインページ def sign_in @user = User.new render layout: "application_not_login" end def sign_out #ここに処理を実装 user_sign_out #サインインページへ遷移 redirect_to sign_in_path and return end # サインイン処理 def sign_in_process # パスワードをmd5に変換 password_md5 = User.generate_password(user_params[:password]) # メールアドレスとパスワードをもとにデータベースからデータを取得 user = User.find_by(email: user_params[:email], password: password_md5) if user # セッション処理 user_sign_in(user) # トップ画面へ遷移する redirect_to top_path and return end end #ユーザー登録処理 def sign_up_process user = User.new(user_params) if user.save #登録が成功したらサインインしてトップページへ user_sign_in(user) redirect_to top_path and return else #登録が失敗したらユーザー登録ページへ flash[:danger] = "ユーザー登録に失敗しました。" redirect_to('/') end end private def user_params params.require(:user).permit(:name, :email, :password) end #現在サインイン中のユーザー情報を返す def current_user if @current_user.nil? @current_user = User.find_by(id: session[:user_id]) else @current_user end end end
users_helper.rb module UsersHelper # ユーザーがサインインしていればtrue, そうでなければfalseを返す def user_signed_in? current_user.present? end # 渡されたユーザーでサインインする def user_sign_in(user) session[:user_id] = user.id end #サインアウトする def user_sign_out session.delete(:user_id) @current_user = nil end # 現在サインイン中のユーザー情報を返す def current_user if @current_user.nil? @current_user = User.find_by(id: session[:user_id]) else @current_user end end # 認証チェック def authorize redirect_to sign_in_path unless user_signed_in? end # サインイン済みならトップページに遷移する def redirect_to_top_if_signed_in redirect_to top_path and return if user_signed_in? end end
models/user.rb class User < ApplicationRecord # データの保存前に、パスワードを暗号化するメソッド(convert_password)を実行するよう設定 before_save :convert_password #バリデーション VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i validates :name, presence: true validates :email, presence: true, format: {with: VALID_EMAIL_REGEX}, uniqueness: true validates :password, presence: true, length:{minimun: 6} # パスワードを暗号化するメソッド def convert_password self.password = User.generate_password(user_params[:password]) end # メールアドレスとパスワードをもとにデータベースからデータを取得 user = User.find_by(email: user_params[:email], password: password_md5) end
皆様、ご意見ありがとうございます。
返信遅くなり申し訳ありません。
users_helper.erの6行目のご指摘がありましたので、修正したところ以下のerrorメッセージに
変わりました。
ちなみにroutes.rbは以下になります。
routes.rb
1Rails.application.routes.draw do 2 root 'users#sign_up' 3 get'top', to:'users#top', as: :top 4 get 'users/top' 5 resources :posts 6 get '/profile/(:id)', to:'users#show',as: :profile 7 get '/profile/edit', to:'users#edit',as: :profile_edit 8 get '/follower_list/(:id)', to:'users#follower_list',as: :follower_list 9 get '/follow_list/(:id)', to:'users#follow_list',as: :follow_list 10 get '/sign_up', to:'users#sign_up',as: :sign_up 11 get '/sign_in', to:'users#sign_in',as: :sign_in 12 get '/sign_out', to:'users#sign_out',as: :sign_out 13 post '/sign_up', to:'users#sign_up_process' 14 post '/sign_in', to:'users#sign_in_process' 15 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 16end
ご意見、ご教授お願い致します。
回答2件
あなたの回答
tips
プレビュー