helper_methodの使い方
helper_methodの使い方
Viewでもhelper_methodを利用したい場合はControllerにhelper_methodを利用。
具体例
ApplicationControllerで以下のようにcurrent_userというメソッドを定義することで、全てのコントローラから利用できるようになります。 さらにhelper_method指定することで全てのViewからも利用できるようになります。
class ApplicationRecord < ActiveRecord::Base helper_method: current_user private @current_user = User.find_by(id: session[:user_id] if session[:user_id]) end end
参考サイト: https://apidock.com/rails/AbstractController/Helpers/ClassMethods/helper_method