あるサイトを作っています。
#教えて欲しいこと
全てのファイルで共通して使える変数をどこで定義すればいいのか
#やりたいこと
ある変数でViewの色を変えたり、保存する場所を変えたりしようと思っています。
#試したこと
現状はURLにクエリを使って設定したりしていますが、form_forで飛ばす時に、クエリが使えなかったり等、やや複雑なことになっています。
以下のsessionのような変数を独自に定義できればと思っています。
rb
1class ApplicationController < ActionController::Base 2 protect_from_forgery with: :exception 3 before_action:set_current_user 4 def set_current_user 5 @current_user = User.find_by(id: session[:user_id]) 6 end 7 8 # authenticate_userメソッドを定義してください 9 def authenticate_user 10 if @current_user == nil 11 flash[:notice] = "ログインが必要です" 12 redirect_to("/login") 13 end 14 end 15 16 def forbid_login_user 17 if @current_user 18 flash[:notice] = "すでにログインしています" 19 redirect_to("/question/edit") 20 end 21 end 22end 23
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。