HELPERS
RUBY METHOD OF THE MONTH
KEVIN MUNC - @MUNCMAN
HELPER METHODS

Located in Helper classes.

Used by Views.
HELPER METHODS

 Located in Helper classes.

 Used by Views.

What if you have a Controller method that would
be useful in...
HELPER METHODS

 Located in Helper classes.

 Used by Views.

What if you have a Controller method that would
be useful in...
C TO THE V


       You have a handy
       Controller method

       It would be useful in a
       View

       What do ...
CONTROLLER METHOD
      INTO VIEW

helper_method(...)
Exposes one or more of the methods defined in
your Controller to your...
RAILSAPI.COM EXAMPLE

class ApplicationController < ActionController::Base
   helper_method :current_user, :logged_in?
   ...
H TO THE C


You have a crafty
method in a Helper

A Controller could
benefit from it

What do you do?
HELPER METHOD
         INTO CONTROLLER
helper(...)
Exposes a proxy to Helper methods
Usage:

 helper :helper_name_minus_he...
MOSTLY ARBITRARY EXAMPLE

class ApplicationController < ActionController::Base
   helper :foo #includes foo_helper methods...
Both methods are declared in
ActionController::Helpers::ClassMethods
REMINDER:
PUT YOUR METHODS
WHERE THEY MAKE
      SENSE.
QUESTIONS?
                   PHOTO CREDITS!
http://www.flickr.com/photos/contusion/2758515669/

http://www.flickr.com/photo...
Helpers (MOTM 2010.03)
Upcoming SlideShare
Loading in...5
×

Helpers (MOTM 2010.03)

577

Published on

Ruby Method of the Month 2010.03. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
577
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide













  • Helpers (MOTM 2010.03)

    1. 1. HELPERS RUBY METHOD OF THE MONTH KEVIN MUNC - @MUNCMAN
    2. 2. HELPER METHODS Located in Helper classes. Used by Views.
    3. 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. 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. 5. C TO THE V You have a handy Controller method It would be useful in a View What do you do?
    6. 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. 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. 8. H TO THE C You have a crafty method in a Helper A Controller could benefit from it What do you do?
    9. 9. HELPER METHOD INTO CONTROLLER helper(...) Exposes a proxy to Helper methods Usage: helper :helper_name_minus_helper_suffix self.class.helper.the_method
    10. 10. 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
    11. 11. Both methods are declared in ActionController::Helpers::ClassMethods
    12. 12. REMINDER: PUT YOUR METHODS WHERE THEY MAKE SENSE.
    13. 13. QUESTIONS? PHOTO CREDITS! http://www.flickr.com/photos/contusion/2758515669/ http://www.flickr.com/photos/njlibraryevents/3309898372/ http://www.flickr.com/photos/betsyweber/3307988944/ http://www.flickr.com/photos/garethjmsaunders/2066690414/ http://www.flickr.com/photos/malingering/97914364/ http://www.flickr.com/photos/cop4cbt/3544420941/ http://www.flickr.com/photos/markhillary/1347412899/ http://www.flickr.com/photos/aspengull/111255602/ http://www.flickr.com/photos/revcyborg/5228173/ http://www.flickr.com/photos/brothermagneto/2224407398/
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×