开发者

Where can I put this code, containing both Javascript and Ruby, so that it can be used by different views in my RoR app?

开发者 https://www.devze.com 2023-01-12 01:26 出处:网络
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.

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消