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'
2

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.