Your SlideShare is downloading. ×
Helpers (MOTM 2010.03)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Helpers (MOTM 2010.03)

526
views

Published on

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

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
526
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide













  • Transcript

    • 1. HELPERS RUBY METHOD OF THE MONTH KEVIN MUNC - @MUNCMAN
    • 2. HELPER METHODS Located in Helper classes. Used by Views.
    • 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. H TO THE C You have a crafty method in a Helper A Controller could benefit from it What do you do?
    • 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. 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. Both methods are declared in ActionController::Helpers::ClassMethods
    • 12. REMINDER: PUT YOUR METHODS WHERE THEY MAKE SENSE.
    • 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/