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

Helpers (MOTM 2010.03)

on

  • 654 views

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.

Statistics

Views

Total Views
654
Slideshare-icon Views on SlideShare
652
Embed Views
2

Actions

Likes
0
Downloads
2
Comments
0

1 Embed 2

http://www.linkedin-ei2.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Helpers (MOTM 2010.03) Helpers (MOTM 2010.03) Presentation Transcript

  • 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 a View?
  • 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?
  • C TO THE V You have a handy Controller method It would be useful in a View What do you do?
  • 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
  • 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.
  • 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_helper_suffix self.class.helper.the_method
  • 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
  • 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/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/