views:

30

answers:

1

I have some javascript code that is used in a few different pages in my website.

The code changes depending on some parameters, which are set in the Ruby on Rails controller.

For example:

acontroller.rb

def aview
    @notes = get_notes
end

aview.html.erb (the actual code is much longer)

<% @notes.each do |note| %>
    var <%=note["something"]%> = new Array(); 
<% end %>

I want the changeable javascript code to be stored in a central place, able to be used by many different views. Initially I thought to put it in a helper method, using a heredoc, but then the code is just inserted into the view, and the Ruby part of the code is not actually run. I can't just put into a private method in the controller, because there's javascript in it. Is there an easy way to do this?

+1  A: 

Could you put it in a partial? That would allow you to share the code wherever you'd like.

Curtis Edmond
Hadn't learned about partials yet, works great. Thanks.
ben