Wednesday, 28 October 2015

how to access controller method as helper_method?


Declare a controller method as a helper. For example, the following makes the current_user controller method available to the view:

class ApplicationController < ActionController::Base
helper_method :current_user

def current_user
@current_user ||= User.find_by_id(session[:user])
end
end

In a view:

Welcome, <%= current_user.name %>

No comments:

Post a Comment