Question

How do I write jQuery code within a Helper function in Rails?

Was it helpful?

Solution

If you have a lot of javascript code then I recommend using a partial instead of trying to place it in strings in a helper method.

<!-- in views/layouts/application.html.erb -->
<% javascript_tag do %>
  <%= render :partial => 'layouts/do_something' %>
<% end %>

<!-- in views/layouts/_do_something.js.erb -->
function do_something() {
  // javascript code goes here
}

You can add ERB tags in that partial as well.

Does this answer your question? I may not have understood it entirely.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top