How to call ApplicationController methods from ApplicationHelper

I want to provide csv links in a view, and I put the csv generation code in ApplicationHelper . However, I get this error:

 undefined method `send_data' for #<#<Class:0x0000010151c708>:0x0000010151a070> 

referring to this:

 send_data content, :type => "text/plain", :filename => filename, :disposition => 'attachment' 

If I put csv code in the controller, it works fine. I was hoping to use an assistant to avoid having to define routes for each controller for which I want to provide csv parameters (I have a group). How can I make send_data (and other necessary methods) available helper?

+45
ruby ruby-on-rails ruby-on-rails-3 controller view-helpers
May 12 '11 at 23:37
source share
2 answers

Use helper_method .

By default, methods in ApplicationController are only available inside controllers.

Add a method to ApplicationController and show it as a helper method with helper_method :

 class ApplicationController < ActionController::Base helper_method :foo def foo "bar" end end 

The foo method is now available for both controllers and views.

+102
May 13 '11 at 3:32 a.m.
source share

If the problem is to make methods in ApplicationHelper available in all controllers, why not add a line

 include ApplicationHelper

to ApplicationController file?

+8
May 29 '11 at 16:07
source share



All Articles