David David - 4 years ago 110
Ruby Question

How to call ApplicationController methods from ApplicationHelper

I want to provide csv links in a view and I placed the csv generating code in

. However I'm getting this error:

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

referencing this:

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

If I place the csv code in a controller it works fine. I was hoping to use the helper to avoid having to define routes for every controller I want to provide csv options for (I have a bunch). How can I make
(and other necessary methods) available to the helper?

Answer Source

Use helper_method.

By default, methods in ApplicationController are only accessible inside the Controllers.

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

class ApplicationController < ActionController::Base

  helper_method :foo

  def foo


Now the foo method is accessible to both Controllers and Views.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download