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

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

    Another option: to create page- or model-specific files, you could create directories inside your assets/javascripts/ folder.

    assets/javascripts/global/
    assets/javascripts/cupcakes
    assets/javascripts/something_else_specific
    

    Your main application.js manifest file could be configured to load its files from global/. Specific pages or groups of pages could have their own manifests which load files from their own specific directories. Sprockets will automatically combine the files loaded by application.js with your page-specific files, which allows this solution to work.

    This technique can be used for style_sheets/ as well.

提交回复
热议问题