SlideShare a Scribd company logo
1 of 14
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/

More Related Content

Similar to Helpers (MOTM 2010.03)

Introduction to ZendX jQuery
Introduction to ZendX jQueryIntroduction to ZendX jQuery
Introduction to ZendX jQuerydennisdc
 
Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Ryan Mauger
 
Heuristic evaluation
Heuristic evaluationHeuristic evaluation
Heuristic evaluationGaurav Bhatia
 
Empowering users: modifying the admin experience
Empowering users: modifying the admin experienceEmpowering users: modifying the admin experience
Empowering users: modifying the admin experienceBeth Soderberg
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress PluginBrad Williams
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress pluginAnthony Montalbano
 
Model View Controller
Model View ControllerModel View Controller
Model View ControllerSV.CO
 
Eclipse Day India 2010 - UI Patterns in Eclipse
Eclipse Day India 2010 - UI Patterns in EclipseEclipse Day India 2010 - UI Patterns in Eclipse
Eclipse Day India 2010 - UI Patterns in Eclipsedeepakazad
 
(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014
(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014
(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014Amazon Web Services
 
Working with AngularJS
Working with AngularJSWorking with AngularJS
Working with AngularJSAndré Vala
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For BegineersM A Hossain Tonu
 
Start Developing Apps for Magnolia CMS
Start Developing Apps for Magnolia CMSStart Developing Apps for Magnolia CMS
Start Developing Apps for Magnolia CMSMagnolia
 
Agile User Centric Design - Lean Usability
Agile User Centric Design - Lean UsabilityAgile User Centric Design - Lean Usability
Agile User Centric Design - Lean UsabilityPoppy Martono
 
Importance of filter in rails
Importance of filter in railsImportance of filter in rails
Importance of filter in railsAndolasoft Inc
 
Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Ryan Mauger
 
Keeping Things Simple In A Growing AngularJS App.
Keeping Things Simple In A Growing AngularJS App.Keeping Things Simple In A Growing AngularJS App.
Keeping Things Simple In A Growing AngularJS App.Brandon Boswell, MBA
 
Introduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin DevelopmentIntroduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin DevelopmentBruce L Chamoff
 
Getting Started with Zend Framework
Getting Started with Zend FrameworkGetting Started with Zend Framework
Getting Started with Zend FrameworkJuan Antonio
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPressEdward Caissie
 

Similar to Helpers (MOTM 2010.03) (20)

Introduction to ZendX jQuery
Introduction to ZendX jQueryIntroduction to ZendX jQuery
Introduction to ZendX jQuery
 
Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)
 
Heuristic evaluation
Heuristic evaluationHeuristic evaluation
Heuristic evaluation
 
Empowering users: modifying the admin experience
Empowering users: modifying the admin experienceEmpowering users: modifying the admin experience
Empowering users: modifying the admin experience
 
Creating Your First WordPress Plugin
Creating Your First WordPress PluginCreating Your First WordPress Plugin
Creating Your First WordPress Plugin
 
Write your first WordPress plugin
Write your first WordPress pluginWrite your first WordPress plugin
Write your first WordPress plugin
 
Model View Controller
Model View ControllerModel View Controller
Model View Controller
 
Eclipse Day India 2010 - UI Patterns in Eclipse
Eclipse Day India 2010 - UI Patterns in EclipseEclipse Day India 2010 - UI Patterns in Eclipse
Eclipse Day India 2010 - UI Patterns in Eclipse
 
(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014
(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014
(APP204) NEW LAUNCH: Introduction to AWS Service Catalog | AWS re:Invent 2014
 
Working with AngularJS
Working with AngularJSWorking with AngularJS
Working with AngularJS
 
Developing WordPress Plugins : For Begineers
Developing WordPress Plugins :  For BegineersDeveloping WordPress Plugins :  For Begineers
Developing WordPress Plugins : For Begineers
 
Start Developing Apps for Magnolia CMS
Start Developing Apps for Magnolia CMSStart Developing Apps for Magnolia CMS
Start Developing Apps for Magnolia CMS
 
Agile User Centric Design - Lean Usability
Agile User Centric Design - Lean UsabilityAgile User Centric Design - Lean Usability
Agile User Centric Design - Lean Usability
 
Importance of filter in rails
Importance of filter in railsImportance of filter in rails
Importance of filter in rails
 
Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)
 
Keeping Things Simple In A Growing AngularJS App.
Keeping Things Simple In A Growing AngularJS App.Keeping Things Simple In A Growing AngularJS App.
Keeping Things Simple In A Growing AngularJS App.
 
Introduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin DevelopmentIntroduction To Simple WordPress Plugin Development
Introduction To Simple WordPress Plugin Development
 
Getting Started with Zend Framework
Getting Started with Zend FrameworkGetting Started with Zend Framework
Getting Started with Zend Framework
 
Dimeji Classified Website
Dimeji Classified WebsiteDimeji Classified Website
Dimeji Classified Website
 
Hooking with WordPress
Hooking with WordPressHooking with WordPress
Hooking with WordPress
 

More from Kevin Munc

Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalKevin Munc
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Kevin Munc
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Kevin Munc
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)Kevin Munc
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)Kevin Munc
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Kevin Munc
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Kevin Munc
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)Kevin Munc
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Kevin Munc
 

More from Kevin Munc (10)

Basic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setIntervalBasic Scheduling with setTimeout & setInterval
Basic Scheduling with setTimeout & setInterval
 
Shellwords
ShellwordsShellwords
Shellwords
 
Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)Number Conversions (MOTM 2010.12)
Number Conversions (MOTM 2010.12)
 
Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)Percent Literals (MOTM 2010.09)
Percent Literals (MOTM 2010.09)
 
cycle (MOTM 2010.07)
cycle (MOTM 2010.07)cycle (MOTM 2010.07)
cycle (MOTM 2010.07)
 
empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)empty?, nil?, blank?, & present? (MOTM 2010.05)
empty?, nil?, blank?, & present? (MOTM 2010.05)
 
Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)Grouping (MOTM 2010.02)
Grouping (MOTM 2010.02)
 
Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)Removing Methods (MOTM 2010.01)
Removing Methods (MOTM 2010.01)
 
The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)The Methods Method and Its Friends (MOTM 2009.08)
The Methods Method and Its Friends (MOTM 2009.08)
 
Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)Ruby's String Slicing (MOTM 2009.07)
Ruby's String Slicing (MOTM 2009.07)
 

Recently uploaded

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 

Recently uploaded (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 

Helpers (MOTM 2010.03)

  • 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.
  • 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
  • 14. 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/

Editor's Notes