简体   繁体   中英

Heroku and Rails 4: some javascript not working

I'm trying to get a javascript item displaying on Heroku Rails 4 applicarion. On my local development machine, everything is running perfectly. Can't get it to work on Heroku.

In my application.html.erb file I have added:

<%= javascript_include_tag "application" %>

and in another view file (events.html.erb) I have:

<script type="text/javascript">
   $(document).ready(function()
   ...code to add data to a timeline using getjson()...
</script>`

which is referring to a timeline.js file in the assets>javascripts folder. When an event is added (using Rails CRUD operation, the javascript code is calling a getJson() method and is adding the event to a timeline. This is working as expected on local machine.

When I push to Heroku, I can add events through rails, but the timeline is not displayed.

My initial idea was that Javascript was not being loaded on Heroku, so therefore I added a simple:

<script type="text/javascript">
   alert('Displays OK')
</script>

call and this displays just fine on Heroku (as well as on my local machine).

When looking at the page source on Heroku, I can see:

<script src="/assets/application-6da332d35efc3706e98f87ead9900f20.js">

so it appears that the timeline.js file is precompiled (it seems a minimized version of the timeline js).

On my local machine, I don't see the precompiled js file, but I see:

<script src="/assets/timeline.js?body=1"></script>
<script src="/assets/timeline.min.js?body=1"></script>

The fact that on Heroku, it displays the javascript alert, but not the timeline, seems to indicate that something is messed up with the javascript code in the precompilation.

I can't see anything obvious in the heroku logs. It says slug compilation was finished successfully and I can't see error messages.

Is there a way I can enforce Heroku to not use the precompiled js file, so I can have on Heroku:

   <script src="/assets/timeline.js?body=1"></script>
   <script src="/assets/timeline.min.js?body=1"></script>

I have the following in production.rb file:

config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = true
config.assets.compress = true
config.assets.digest = true
config.assets.debug = false

Hope someone can help me on this. This has been bugging me for couple of days now. Any help appreciated. Thanks!

I think you need to specify that timeline.js must be precompiled. On config/environments/production.rb, add this :

config.assets.precompile += %w( timeline.js )

In you view, include you timeline js with :

<%= javascript_include_tag "timeline" %>

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