在heroku上部署的ruby rails应用程序没有像预期的那样运行Javascript [英] Javascript not running as expected in ruby rails app deployed on heroku

查看:81
本文介绍了在heroku上部署的ruby rails应用程序没有像预期的那样运行Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Rails 4并在Heroku上部署了一个应用程序。我一直在本地预编译我的资产,但是我的JavaScript没有按预期工作。



我的application.js文件看起来像这样('custom-scripts'包含我所有的重要功能):

  // =要求jquery 
// =要求jquery.turbolinks
// =要求jquery_ujs
// =要求turbolinks
// =需要jquery.slick
// =需要定制脚本
// = require_tree。

在Resources选项卡下的Chrome开发工具中,我看到Frames / appname / Scripts / application.js存在将'custom-scripts.js'中的代码放在文件底部。在Elements选项卡中,脚本标记链接到/assets/application.js,但在appname / assets /中的Sources选项卡下,我没有看到application.js,只有application.css。
我使用的是<%= javascript_include_tag'应用程序'%> 标记,因此它应该正确链接正确?



我一直在阅读有关Assets管道的信息,并且我看到的所有信息都表明一切都应该起作用。我写了一个 test()函数,它只是在我的custom-scripts文件中返回一个console.log函数,并且在Console选项卡中调用时,它返回'function not defined'这告诉我我的功能甚至没有出于某种原因运行。任何想法可能是错的?

解决方案

我认为这更像是一个肮脏的修复,但它可以工作,所以现在这是我的解决方案;所以我从application.js中删除了 // = require_tree。 // = require custom-scripts ,并添加了<%= javascript_include_tag'custom-scripts'%> 到我的application.html.erb文件,然后运行 rake assets:precompile 再次推到github& heroku和巴姆,其奇妙的工作。

I'm using Rails 4 and have an app deployed on Heroku. I've been precompiling my assets locally, but my javascript isn't working as expected.

my application.js file looks like this('custom-scripts' has all my important functions in it):

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
//= require jquery.slick
//= require custom-scripts
//= require_tree .

On Chrome's dev tools under the Resources tab I see that Frames/appname/Scripts/application.js exists with the code in 'custom-scripts.js' at the bottom of the file. In the Elements tab, the script tag is linking to /assets/application.js but under the Sources tab inside appname/assets/ I don't see application.js only application.css. I'm using the <%= javascript_include_tag 'application' %> tag so it should link correctly right?

I've been reading about the Assets pipeline and everything I'm seeing says that everything should be working. I wrote a test() function that just returns a console.log function in my custom-scripts file and when called in the Console tab, it returns 'function not defined' so that tells me my functions aren't even running for some reason. Any idea what could be wrong?

解决方案

I think this is more of a dirty fix, but it works, so for now this is my solution; So I removed //= require_tree . and //= require custom-scripts from application.js and added <%= javascript_include_tag 'custom-scripts' %> to my application.html.erb file then ran rake assets:precompile again, pushed to github & heroku and bam, its working wonderfully.

这篇关于在heroku上部署的ruby rails应用程序没有像预期的那样运行Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆