简体   繁体   中英

Rails Asset pipeline manage issue

I'm making a page that needs real-time data.

So, it needs a repeatable ajax communication after page loads.

like this,

jQuery ->
  setInterval -> 
    $.ajax
      url: "some url"
      success: (data, textStatus, jqXHR) ->
       #handle the data here

the problem occurs here,

i only need this code in one page

but because of this in application.js

require_tree .

it loads the whole coffeescript(javascript) code in every pages.

How do i include this coffeescript file in only one page?

Do i have to manually include the javascript in .erb file like this?

<%=javascript_include_tag 'what_i_want'%>

Is this the right way?

Include this in your erb file.

<%= javascript_include_tag 'what_i_want' %>

If you will add it too your erb file directly you need to add your js file in config/initializers/assets.rb

Rails.application.config.assets.precompile += ['what_i_want.js']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM