よろしくお願いします。
current_user.idがテーブルにあるかどうかで条件分岐させるのはどうでしょうか。
0Like
画像投稿アプリを作成しておりまして、ログインしていないユーザーが、URLの直接入力にて投稿編集ページに
飛べないようにauthenticate_user!メソッドを使用したのですが、trainerとcliantの2パターンでユーザー
登録を分けているため、例えばcliantがログインしている場合はauthenticate_trainer!が働いてしまい、
うまくいきません。
deviseで複数モデル管理をしている場合のauthenticate_user!メソッドの記述方法についてご教示
いただきたいです。
class ApplicationController < ActionController::Base
before_action :basic_auth
before_action :authenticate_trainer!, except: [:index, :show]
before_action :authenticate_cliant!, except: [:index, :show]
def after_sign_in_path_for(resource)
posts_path(resource)
end
def basic_auth
authenticate_or_request_with_http_basic do |username, password|
username == ENV["BASIC_AUTH_USER"] && password == ENV["BASIC_AUTH_PASSWORD"]
end
end
end
class ApplicationController < ActionController::Base
before_action :basic_auth
before_action :authenticate_any!
def authenticate_any!
if cliant_signed_in?
true
else
authenticate_trainer!
end
end
def after_sign_in_path_for(resource)
posts_path(resource)
end
def basic_auth
authenticate_or_request_with_http_basic do |username, password|
username == ENV["BASIC_AUTH_USER"] && password == ENV["BASIC_AUTH_PASSWORD"]
end
end
end
上記のように自分でメソッドを作って記述もしてみましたがうまくいかず、、、
アドバイスお願い致します。
よろしくお願いします。
current_user.idがテーブルにあるかどうかで条件分岐させるのはどうでしょうか。
@takuyahori
Questioner拓也さんの記述とは違いますが、deviseのヘルパーメソッドにあるcurrent_user
を使った例だと以下のようになるかと思います。
current_user(deviseのヘルパーで、現在ログインしているuserの情報が取得できる)のidがデータベースにあるかどうかであれば、
if current_user.id == User.find[params[:current_user.id]
みたいな感じで、ログインしているuserのidと、Userモデルの中にログインしているuserのidがあるのかどうかという感じになるかなと思いました。
@takuyahori
Questioner@takuyahori
Questioner