# Using Helpers inside a Controller

**URL:** <https://www.ruby-forum.com/t/using-helpers-inside-a-controller/109439>\
**Category:** Rails\
**Created:** [July 15, 2007, 12:04am UTC](https://www.ruby-forum.com/t/using-helpers-inside-a-controller/109439 "2007-07-15T00:04:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [July 15, 2007, 12:04am UTC](https://www.ruby-forum.com/t/using-helpers-inside-a-controller/109439/1 "2007-07-15T00:04:10Z")

</div>

I want to use helpers inside a controller method for an AJAX  
application. After the page is loaded and displayed, there will be an  
asynchronous javascript request to load additional resources such as  
html forms and other data.

The controller which handles the request will collect the resources and  
send them back in json format.

def get\_resources

data=Hash.new  
data[:form1]=form\_helper1()  
data[:form2]=form\_helper2()  
…etc…

render :text data.to\_json, :layout=\>false  
end

This works great if I’m loading up the data hash with pure data, but I  
can’t collect html elements such as forms that require helpers because I  
can’t call the helper inside of the controller.

I searched the web for a workaround and came across code to be defined  
insider your controller:

def help  
Helper.instance  
end

class Helper  
include Singleton  
include ActionView::Helpers::MyHelper  
end

But no dice. I get an uninitialized constant error for  
ActionView::Helpers::MyHelper. If I instead include just MyHelper, then  
rails complains that the helpers that are used inside MyHelper are now  
undefined.

I really don’t want to construct the forms inside my controller, but  
that’s what I’m going to have to do if I can’t figure out how to call my  
helpers.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [July 15, 2007, 3:16am UTC](https://www.ruby-forum.com/t/using-helpers-inside-a-controller/109439/2 "2007-07-15T03:16:47Z")

</div>

On 7/14/07, J. Sorenosn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> can’t call the helper inside of the controller.

If I am understanding you correctly, that means you want your AJAX  
request to return HTML markup instead of data, right? If so, you  
should really use a view component like a partial, which can itself  
use helpers just fine. In the second edition of Agile Web D.  
With Rails, Chapter 9 (“Task D: Add a Dash of AJAX”) has an example  
of this technique.

Controllers should be about executing business logic and providing  
data to the view, not being the view themselves. Helpers are _view_  
helpers not _controller_ helpers.

Craig McClanahan

---

<div class="post-metadata">

**Author:** ![sorenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@sorenson](https://www.ruby-forum.com/u/sorenson)\
**Post date:** [July 15, 2007, 8:48am UTC](https://www.ruby-forum.com/t/using-helpers-inside-a-controller/109439/3 "2007-07-15T08:48:23Z")

</div>

> class Helper  
> include Singleton  
> include ActionView::Helpers::MyHelper  
> end
> 
> But no dice. I get an uninitialized constant error for  
> ActionView::Helpers::MyHelper. If I instead include just MyHelper, then  
> rails complains that the helpers that are used inside MyHelper are now  
> undefined.

i had a certain controller and i added a helper with no probs, like  
below: ( i had the following files:)

…/helpers/my\_helper.rhtml # class MyHelper  
…/controllers/example\_controller.rhtml # class ExampleController

and then in example\_controller.rhtml i did this:

require ‘my\_helper’ #LOOK HERE

class ExampleController

include MyHelper #AND HERE
