helper_method
defined in rails/actionpack/lib/abstract_controller/helpers.rb
Declare a controller method as a helper. For example, the following
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
then in view
<% if logged_in? -%>Welcome, <%= current_user.name %><% end -%>
helper
The helper
class method can take a series of helper module names, a block, or both.
Parameters *args – Module, Symbol, String, :all block – A block defining helper methods
原文地址:Rails helper and helper_method in Controller, 感谢原作者分享。 愚者用肉体监视心灵,智者用心灵监视肉体