We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

vanne on 04/18/07


Tagged

rails ruby


Versions (?)


Use helpers in controllers or models


Published in: Ruby 


URL: http://snippets.dzone.com/posts/show/1799

This is an easy to use any helpers that rails provides in any other place besides views and view helpers

  1. # create a new file inside lib/ and call it helpers.rb
  2. # paste the following:
  3.  
  4. def help
  5. Helper.instance
  6. end
  7.  
  8. class Helper
  9. include Singleton
  10. # look inside ActionView::Helpers to include any other helpers that you might need
  11. include ActionView::Helpers::DateHelper
  12. include ActionView::Helpers::TextHelper
  13. end
  14.  
  15. # then in any model or controller:
  16. require 'lib/helpers'
  17.  
  18. # to use:
  19. # help.name_of_helper
  20. # EX: help.pluralize 10, "person"

Report this snippet 

You need to login to post a comment.