Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 View helper method in Model

I have a class method in my model, and I need to access a method from one of my view helpers. Currently I am including include TalkHelper, but I still get a NoMethodError.

like image 697
LanguagesNamedAfterCofee Avatar asked Sep 18 '11 23:09

LanguagesNamedAfterCofee


1 Answers

In your model, you can do something like the following:

ApplicationController.helpers.your_helper_method

OR

YourController.helpers.your_helper_method

The best solution is to refactor your code so that you don't need to call view helper code at all from models. It is not the RoR way. As others point out, you could extract the helper code to lib folder.

See this for more info:

http://railscasts.com/episodes/132-helpers-outside-views

like image 51
Innerpeacer Avatar answered Oct 27 '22 11:10

Innerpeacer