# Writing a Helper function

**URL:** <https://www.ruby-forum.com/t/writing-a-helper-function/102214>\
**Category:** Rails\
**Created:** [August 13, 2006, 2:19am UTC](https://www.ruby-forum.com/t/writing-a-helper-function/102214 "2006-08-13T02:19:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [August 13, 2006, 2:19am UTC](https://www.ruby-forum.com/t/writing-a-helper-function/102214/1 "2006-08-13T02:19:09Z")

</div>

I’ve finally gotten to the point where I want to write my own helper  
function to call from my views. I used the script/generate to create my  
Controller so I also got the helper file.

I defined the method I wanted to use in the helper rb file, but when I  
try to call them from the view I get a NoMethodError raised.

Is there anything else I have to do besides creating the method in the  
Helper module in the app/helpers directory and call it from a view? The  
on-line documentation on helpers seems a bit light.

Thanks

Dave

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [August 13, 2006, 2:55am UTC](https://www.ruby-forum.com/t/writing-a-helper-function/102214/2 "2006-08-13T02:55:18Z")

</div>

Which file are you putting the helper function into? You might want to  
include

\*The file that the helper method is in  
\*The line that calls the method in the view  
\*The error that you are getting as a result

In your error report, it’s a bit hard to troubleshoot without more data.

Helper functions work for me. You can try and put your method in  
application\_helper.rb. Make sure you are not putting your def into the  
controller application.rb.

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/writing-a-helper-function/102214/3 "2006-08-15T14:18:36Z")

</div>

Hi –

On Sun, 13 Aug 2006, Dave Fox wrote:

> I’ve finally gotten to the point where I want to write my own helper  
> function to call from my views. I used the script/generate to create my  
> Controller so I also got the helper file.
> 
> I defined the method I wanted to use in the helper rb file, but when I  
> try to call them from the view I get a NoMethodError raised.
> 
> Is there anything else I have to do besides creating the method in the  
> Helper module in the app/helpers directory and call it from a view? The  
> on-line documentation on helpers seems a bit light.

Make sure you define the method inside the module definition:

module SomeHelper  
def help\_me  
…  
end  
end

and not before or after it. I’m just guessing that might be the  
problem – it’s a not unheard-of cause of that NoMethodError from  
helpers.

David

–  
[http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
----\> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! \<-----  
[http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack’s][Web]log  
[Ruby for Rails](http://www.manning.com/black) =\> book, Ruby for Rails  
[http://www.rubycentral.org](http://www.rubycentral.org) =\> Ruby Central, Inc.
