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?