2

I have this Helper in my ApplicationHelper

module ApplicationHelper
  def sort_url
    controller_name = params[:controller] 
    url = controller_name + '/sort' 
  end
end

And in application.js.erb

url: "<%= sort_url %>", 

Error:

undefined local variable or method `sort_url'
Pedro Souza
  • 345
  • 1
  • 3
  • 14

1 Answers1

1

That is because the ApplicationHelper is only included in views by default.

The javascript file is not a view.

If you really want to use helpers in the view, you can use

<% environment.context_class.instance_eval { include MyHelper } %>

For more information on a related question, see: Using a Rails helper method within a javascript asset

Community
  • 1
  • 1
ronalchn
  • 12,225
  • 10
  • 51
  • 61