# Basic Question - From where are helpers available

**URL:** <https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539>\
**Category:** Rails\
**Created:** [January 9, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539 "2008-01-09T23:56:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Decker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@Decker](https://www.ruby-forum.com/u/Decker)\
**Post date:** [January 9, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539/1 "2008-01-09T23:56:50Z")

</div>

Newbie question – I thought that helpers were available in both views  
and controllers. The methods I’m putting in application\_helper.rb  
don’t seem to be available in the controllers. Am I missing something?

---

<div class="post-metadata">

**Author:** ![Decker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@Decker](https://www.ruby-forum.com/u/Decker)\
**Post date:** [January 10, 2008, 12:05am UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539/2 "2008-01-10T00:05:37Z")

</div>

The helper methods are only available to the views.

Method heirarchy goes a bit like this:

Model  
Controller  
Helper  
View

With all the elements being able to access any method from any element  
above  
it.

Don’t put model code in the view because it will slow your application  
down.  
If you’re doing something like a Model.find, stick it in your  
controller. If  
you’re defining a new method for an object of Model, stick it in the  
model  
file. Helpers are methods you can call from the view to dry up your  
views.  
For example if you wanted to duplicate the e’s in a sentence such as:  
“The  
quick brown fox jumped over the lazy dog”, put this in your view:

\<%= duplicate\_e(“The quick brown fox jumped over the lazy dog”) %\>

and this in your helper:

def duplicate\_e(string)  
string.gsub(“e”,“ee”)  
end

On Jan 10, 2008 9:26 AM, Decker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Newbie question – I thought that helpers were available in both views  
> and controllers. The methods I’m putting in application\_helper.rb  
> don’t seem to be available in the controllers. Am I missing something?
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Decker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@Decker](https://www.ruby-forum.com/u/Decker)\
**Post date:** [January 10, 2008, 12:14am UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539/3 "2008-01-10T00:14:25Z")

</div>

Ryan,

You’re the best!

Thanks!

---

<div class="post-metadata">

**Author:** ![Decker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@Decker](https://www.ruby-forum.com/u/Decker)\
**Post date:** [January 10, 2008, 2:59am UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539/4 "2008-01-10T02:59:55Z")

</div>

On Jan 9, 2008, at 5:04 PM, Ryan B. wrote:

> The helper methods are only available to the views.

But if you want, you can define the method in the controller and  
“export” it using

helper\_method()

and then you can use it in the view.

So in your controller, you could do something like

helper\_method :my\_method

def my\_method

end

[http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers](http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers)

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![Decker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@Decker](https://www.ruby-forum.com/u/Decker)\
**Post date:** [January 10, 2008, 5:02am UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539/5 "2008-01-10T05:02:10Z")

</div>

If you put a method in the application.rb it should be available to all  
controllers that descend from ApplicationController, but referencing it  
in a  
view won’t work.

On Jan 10, 2008 2:24 PM, Jeff P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > helper\_method()
> 
> thanks,  
> jp
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![Decker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@Decker](https://www.ruby-forum.com/u/Decker)\
**Post date:** [January 10, 2008, 5:25am UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539/6 "2008-01-10T05:25:32Z")

</div>

On Jan 9, 2008, at 9:54 PM, Jeff P. wrote:

> > helper\_method()
> 
> thanks,  
> jp

I just put this in application.rb

helper\_method :say\_hi

def say\_hi  
“hi”  
end

and create a test.rhtml for an arbitrary controller that consisted of

\<%= say\_hi %\>

and when I ran [http://localhost:3000/arbitrary\_controller/test](http://localhost:3000/arbitrary_controller/test), I saw  
“hi”.

So it seems that it does, in fact, work. At least with Rails 1.2.3.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![Decker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@Decker](https://www.ruby-forum.com/u/Decker)\
**Post date:** [January 10, 2008, 4:54am UTC](https://www.ruby-forum.com/t/basic-question-from-where-are-helpers-available/125539/7 "2008-01-10T04:54:18Z")

</div>

Phillip K. wrote:

> On Jan 9, 2008, at 5:04 PM, Ryan B. wrote:
> 
> > The helper methods are only available to the views.
> 
> But if you want, you can define the method in the controller and  
> “export” it using
> 
> helper\_method()
> 
> and then you can use it in the view.
> 
> So in your controller, you could do something like
> 
> helper\_method :my\_method
> 
> def my\_method
> 
> end
> 
> [http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers](http://wiki.rubyonrails.org/rails/pages/HowtoWorkWithHelpers)
> 
> Peace,  
> Phillip

Hmmmn, what if you put a method in the “application.rb” controller and  
then used helper\_method() on it? Would it then be available to all  
controllers and all views?

thanks,  
jp
