Rails helper and helper_method in Controller

helper_method

defined in rails/actionpack/lib/abstract_controller/helpers.rbDeclare 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

相关文章:

你感兴趣的文章:

标签云: