Ruby on Rails 4: How to include Javascript files in Rails web application?

后端 未结 4 1408
余生分开走
余生分开走 2020-12-14 08:01

I am building a Rails 4 web application and I want to include some .js files into my application. Is it possible to directly add JavaScript file to my Rails ..app/asse

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-14 08:14

    You have to explicitly add jquery and bootstrap in application.js, whereas you will automatically include custom files requiring the directory tree. If you take a look at your application.js there should be a manifest saying that. This is how your application.js should look like with bootstrap and jquery

    // This is a manifest file that'll be compiled into application.js, which will include all the files
    // listed below.
    //
    // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
    // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
    //
    // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
    // compiled file.
    //
    // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
    // about supported directives.
    //
    //= require jquery
    //= require jquery_ujs
    //= require bootstrap
    //= require bootstrap/affix
    //= require bootstrap/alert
    //= require bootstrap/button
    //= require bootstrap/carousel
    //= require bootstrap/collapse
    //= require bootstrap/dropdown
    //= require bootstrap/tab
    //= require bootstrap/transition
    //= require bootstrap/scrollspy
    //= require bootstrap/modal
    //= require bootstrap/tooltip
    //= require bootstrap/popover
    //= require turbolinks
    //= require_tree .
    

    No need to add your custom file in this list as you are requiring all the files at the same level of your application.js (or lower in the directory tree) with the last directive (require tree .)

提交回复
热议问题