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

前端 未结 29 2117
一生所求
一生所求 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:39

    I see that you've answered your own question, but here's another option:

    Basically, you're making the assumption that

    //= require_tree .
    

    is required. It's not. Feel free to remove it. In my current application, the first I'm doing with 3.1.x honestly, I've made three different top level JS files. My application.js file only has

    //= require jquery
    //= require jquery_ujs
    //= require_directory .
    //= require_directory ./api
    //= require_directory ./admin
    

    This way, I can create subdirectories, with their own top level JS files, that only include what I need.

    The keys are:

    1. You can remove require_tree - Rails lets you change the assumptions it makes
    2. There's nothing special about the name application.js - any file in the assets/javascript subdirectory can include pre-processor directives with //=

    Hope that helps and adds some details to ClosureCowboy's answer.

    Sujal

提交回复
热议问题