# [rails]helper相關筆記 ###### tags: `rails` ## 線上資源 https://ithelp.ithome.com.tw/articles/10161314 ## helper_method https://apidock.com/rails/ActionController/Helpers/ClassMethods/helper_method 在ApplicationContrller內 使用helper_method語法可讓該方法於view中及controller中使用: app/controllers/application_controller.rb ```ruby= class ApplicationController < ActionController::Base helper_method :current_user, :logged_in? def current_user @current_user ||= User.find_by_id(session[:user]) end def logged_in? current_user != nil end end ``` ## 自訂helpers 在app/helpers底下新增rb檔案 並於application_controller中引用: app/helpers/test_helper.rb ```ruby= module TestHelper def somefn end end ``` app/controllers/application_controller.rb ```ruby= class ApplicationController < ActionController::Base include TestHelper # ... end ```