一些javascript无法正常工作

一些javascript无法正常工作

本文介绍了Heroku和Rails 4:一些javascript无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Heroku Rails 4应用程序上显示一个javascript项目。在我的本地开发机器上,一切都运行得很好。无法让它在Heroku上运行。

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.

在我的application.html.erb文件中,我添加了:

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

<%= javascript_include_tag "application" %>

和另一个视图文件(events.html.erb)我有:

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>`

指的是assets> javascripts文件夹中的timeline.js文件。添加事件时(使用Rails CRUD操作) ,javascript代码调用getJson()方法,并将事件添加到时间轴。这在本地计算机上正常工作。

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.

当我推送到Heroku时,我可以通过rails添加事件,但不会显示时间轴。

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

我最初的想法是没有在Heroku上加载Javascript,所以我添加了一个简单的:

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>

调用,这在Heroku(以及我的本地机器)上显示正常)。

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

在Heroku上查看页面源代码时,我可以看到:

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

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

因此看来timeline.js文件是预编译的(它似乎是时间线js的最小化版本) )。

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

在我的本地机器上,我没有看到预编译的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>

在Heroku上,它显示javascript警报,但不显示时间轴,这似乎表明有些东西搞砸了预编译中的javascript代码。

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.

我在heroku日志中看不到任何明显的东西。它说slug编译成功完成,我看不到错误消息。

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

有没有办法可以强制Heroku不使用预编译的js文件,所以我可以拥有Heroku:

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>

我在production.rb文件中有以下内容:

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!

推荐答案

我认为您需要指定必须预编译timeline.js。
在config / environments / production.rb上,添加:

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 )

在你看来,包括你的时间线js:

In you view, include you timeline js with :

<%= javascript_include_tag "timeline" %>

这篇关于Heroku和Rails 4:一些javascript无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:34