0

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?

Canna
  • 3,614
  • 5
  • 28
  • 33
  • Either you can include the script in your .erb file itself or create a separate js file and call that js file using your above rails tag. – Naveen Kumar May 14 '15 at 11:56
  • 1
    I cant' understand the second one. create a separate js file and call that js file using your above rails tag. – Canna May 14 '15 at 11:59
  • create a new js file something like real_time.js and add your ajax script in that file and then call js file using <%= javascript_include_tag 'real_time' %> – Naveen Kumar May 14 '15 at 12:03
  • 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''] – Pardeep Dhingra May 14 '15 at 12:13
  • Find a good solution. http://stackoverflow.com/questions/6167805/using-rails-3-1-where-do-you-put-your-page-specific-javascript-code – Canna May 14 '15 at 12:20

1 Answers1

0

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']
Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56