The right way to include page-specific JavaScript in Rails

后端 未结 5 772
青春惊慌失措
青春惊慌失措 2021-01-03 06:50

I want to include this, for example:

jQuery(document).ready(function($) {
    $(\'#my-modal\').modal(options)
});

In one specific place in

5条回答
  •  爱一瞬间的悲伤
    2021-01-03 07:51

    One of the solution is to insert our javascript in to its own file: Reference Link

    For example :

    // app/assets/javascripts/alert.js

    alert("My example alert box.");
    

    And including this file only in the view we want it to execute:

    <%# app/views/page/contact.html.erb %>
    <%= javascript_include_tag "alert" %>
    

    Contact

    This is the contact page

    And don’t forget to include your new file in the list of files to be compiled:

    # config/environments/production.rb
    config.assets.precompile += %w( alert.js )
    

提交回复
热议问题