48

I want to provide csv links in a view and I placed the csv generating code in ApplicationHelper. 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 send_data (and other necessary methods) available to the helper?

David
  • 7,132
  • 6
  • 38
  • 61
  • Untested Thought: Check what the class name it is being called on. Does renaming the method to something else help (it could be that send_data is a already-defined method by rails?) – Zabba May 13 '11 at 00:28
  • Yes it is a rails-defined method, but it seems to be accessible only though controllers. – David May 13 '11 at 00:54

2 Answers2

112

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
    "bar"
  end

end

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

Harish Shetty
  • 62,259
  • 19
  • 144
  • 196
10

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

include ApplicationHelper

to the ApplicationController file?

GeorgeW
  • 566
  • 5
  • 17