I'm happened to write create one file in lib folder and I want to use TextHelper in that file. How can I make Texthelper available?
Suggestions appreciated, Thanks,
I'm happened to write create one file in lib folder and I want to use TextHelper in that file. How can I make Texthelper available?
Suggestions appreciated, Thanks,
Hi,
The idea of MVC (Model View Controller) is that only the views should have to deal with formatting/outputting text. So Rails makes it VERY hard to use TextHelper from a class that you would put into the /lib folder.
What does the "file" in /lib do? Is it a class? A module?
See http://en.wikipedia.org/wiki/Model-view-controller for an overview of MVC. The idea is that text manipulation happens in the view. If your new class is a subclass of active record then the controller should take the data from your class and make it available to the view (in the template files).
If your new class is a sub class of action controller then it should also use the view to manipulate the text.
As the TextHelper readme states, "These helper methods extend ActionView making them callable within your template files."
Regards,
Larry
Actually it's not that hard at all. You can just include the TextHelper
module from your class.
class MyLib
include ActionView::Helpers::TextHelper
def five_things(x)
pluralize 5, x
end
end
>> MyLib.new.five_things "dog"
=> "5 dogs"
That's from a class I defined in lib
, and output from a script/console
session to make sure it all plays nice.