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

前端 未结 29 2300
一生所求
一生所求 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条回答
  •  猫巷女王i
    2020-11-22 11:22

    Here's how to do it especially if you don't have to execute tons of libraries for your specific page, but only to run a few hundreds lines of JS more or less.

    Since it's perfectly fine to embed Javascript code into HTML, just create under app/views shared.js directory and place there your page/pages specific code inside my_cool_partial.html.erb

    
    

    So now from wherever you want you simply do:

      = render :partial => 'shared.js/my_cool_partial'
    

    And that's it, k?

提交回复
热议问题