3. HELPER METHODS
Located in Helper classes.
Used by Views.
What if you have a Controller method that would
be useful in a View?
4. HELPER METHODS
Located in Helper classes.
Used by Views.
What if you have a Controller method that would
be useful in a View?
What if you have a Helper method that would be
useful in a Controller?
5. C TO THE V
You have a handy
Controller method
It would be useful in a
View
What do you do?
6. CONTROLLER METHOD
INTO VIEW
helper_method(...)
Exposes one or more of the methods defined in
your Controller to your Views
Usage:
helper_method :useful_method, :nifty_method
7. RAILSAPI.COM EXAMPLE
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
<% if logged_in? -%>Welcome, <%= current_user.name %><% end -%>
See http://www.idolhands.com/ruby-on-rails/guides-tips-and-tutorials/confusion-over-convention-helper-methods
for info on helper scoping.
8.
9. H TO THE C
You have a crafty
method in a Helper
A Controller could
benefit from it
What do you do?
10. HELPER METHOD
INTO CONTROLLER
helper(...)
Exposes a proxy to Helper methods
Usage:
helper :helper_name_minus_helper_suffix
self.class.helper.the_method
11. MOSTLY ARBITRARY EXAMPLE
class ApplicationController < ActionController::Base
helper :foo #includes foo_helper methods (including bar_baz).
def do_something
bar_baz #use a helper method same as a mixed in method.
self.class.helpers.link_to(text,path) #use a specific helper method.
end
end
12. Both methods are declared in
ActionController::Helpers::ClassMethods