Using Rails 3.1, where do you put your “page specific” JavaScript code?

前端 未结 29 2345
一生所求
一生所求 2020-11-22 11:08

To my understanding, all of your JavaScript gets merged into 1 file. Rails does this by default when it adds //= require_tree . to the bottom of your appl

29条回答
  •  天命终不由人
    2020-11-22 11:26

    ryguy's answer is a good answer, even though its been downvoted into negative points land.

    Especially if you're using something like Backbone JS - each page has its own Backbone view. Then the erb file just has a single line of inline javascript that fires up the right backbone view class. I consider it a single line of 'glue code' and therefore the fact that its inline is OK. The advantage is that you can keep your "require_tree" which lets the browser cache all the javascript.

    in show.html.erb, you'll have something like:

    <% provide :javascript do %>
      <%= javascript_include_tag do %>
        (new app.views.ProjectsView({el: 'body'})).render();
      <% end %>
    <% end do %>
    

    and in your layout file, you'll need:

    <%= yield :javascript %>
    

提交回复
热议问题