Ruby-on-rails 为什么我的 rails 资产管道中的 js 文件没有被编译?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18005115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are js files in my rails asset pipeline not being compiled?
提问by andersr
I am experiencing a problem similar to this question: Rails asset pipeline not including required files in application.js manifestHowever, that question was closed, so I am re-asking it, but with the specifics of my situation.)
我遇到了一个类似于这个问题的问题: Rails 资产管道不包括 application.js manifest 中的必需文件但是,该问题已关闭,所以我重新询问它,但要考虑我的具体情况。)
Environment:
环境:
Ruby 2.0.0, Rails 3.2.8, OSX 10.7.5, Chrome 28.0.1500.95
Ruby 2.0.0、Rails 3.2.8、OSX 10.7.5、Chrome 28.0.1500.95
The main problem I am experiencing
我遇到的主要问题
Files placed in "/app/assets/javascripts" do not appear to be compiling to or appearing in "/public/assets"
放置在“/app/assets/javascripts”中的文件似乎没有编译到或出现在“/public/assets”中
An Example
一个例子
If I place a file such as this test.js file in app/assets/javascripts:
如果我在 app/assets/javascripts 中放置一个文件,例如这个 test.js 文件:
alert("Hello!");
and then reload a page in my app, this alert should appear. (I am replicating what Ryan Bates does in his Railscast on the Asset Pipelineat ~6:30 into the video) However, no alert appears.
然后在我的应用程序中重新加载页面,应该会出现此警报。(我正在将 Ryan Bates 在他的Railscast on the Asset Pipeline中大约6:30复制到视频中)但是,没有出现警报。
Debugging attempts
调试尝试
Some tests I did to try to debug this.
我做了一些测试来尝试调试它。
- (As recommended by @Aidan below) If I "rm -rf public/assets", then add "javascript_include_tag 'test'" to my layout, the alert doesappear. However, maybe because I'm a n00b, I don't know how that helps me debug the problem.
- If I add //= require_test to app/javascripts/application.js to my manifest file, this does notresult in the alert appearing.
- (所推荐的@Aidan下文)如果我“室射频公共/资产”,然后添加“javascript_include_tag‘测试’”我的布局,警报不会出现。但是,也许因为我是 n00b,我不知道这如何帮助我调试问题。
- 如果我添加// = require_test到应用程序/ JavaScript的/ application.js中我的清单文件,这并不会导致警报出现。
My manifest file (app/javascripts/application.js)
我的清单文件 (app/javascripts/application.js)
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
// removed /sitewide from require_tree
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
My application.rb file
我的 application.rb 文件
# Enable the asset pipeline
config.assets.enabled = true
Rails console output for "y Rails.application.config.assets.paths"
“y Rails.application.config.assets.paths”的 Rails 控制台输出
"/Users/Anders/Dropbox/dev/suggestion-box-app/app/assets/images",
"/Users/Anders/Dropbox/dev/suggestion-box-app/app/assets/javascripts",
"/Users/Anders/Dropbox/dev/suggestion-box-app/app/assets/stylesheets",
"/Users/Anders/.rvm/gems/ruby-2.0.0-p195@suggestion-box-app/gems/coffee-rails-3.2.2/lib/assets/javascripts",
"/Users/Anders/.rvm/gems/ruby-2.0.0-p195@suggestion-box-app/gems/jquery-rails-3.0.4/vendor/assets/javascripts"
My views/layouts/application.html.haml file
我的 views/layouts/application.html.haml 文件
!!! 5
%html
%head
%meta{:charset => "utf-8"}/
%meta{:content => "width=device-width", :name => "viewport"}/
%meta{:content => "yes", :name => "apple-mobile-web-app-capable"}/
%title My App
%script{:type=>"text/javascript", :src=>"//use.typekit.net/wjb6ybj.js"}
:javascript
try{Typekit.load();}catch(e){}
= stylesheet_link_tag "screen", :media => "all"
= csrf_meta_tags
= javascript_include_tag "application"
%body
#container
= render "layouts/flashes"
= yield
= render "layouts/footer"
Source from an app page viewed in localhost:3000
来自在 localhost:3000 中查看的应用程序页面的来源
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta content='width=device-width' name='viewport'>
<meta content='yes' name='apple-mobile-web-app-capable'>
<title>My app</title>
<script src='//use.typekit.net/wjb6ybj.js' type='text/javascript'></script>
<script>
try{Typekit.load();}catch(e){}
</script>
<link href="/assets/screen.css?body=1" media="all" rel="stylesheet" type="text/css" />
<meta content="authenticity_token" name="csrf-param" />
<meta content="FqMtH+rs6or9HRx+RL4bWpQyLTNPM8BKLrcf/xZknP8=" name="csrf-token" />
<script src="/assets/application.js?body=1" type="text/javascript"></script>
</head>
<body>
<div id='container'>
....
</div>
</body>
</html>
What I see when viewing "localhost:3000/assets/application.js"
我在查看“localhost:3000/assets/application.js”时看到的
/*!
* jQuery JavaScript Library v1.9.1
* http://jquery.com/
*
* Includes Sizzle.js
* http://sizzlejs.com/
*
* Copyright 2005, 2012 jQuery Foundation, Inc. and other contributors
* Released under the MIT license
* http://jquery.org/license
*
* Date: 2013-2-4
*/
(function(e,t){function P(e){var t=e.length,n=b.type(e);return b.isWindow(e)?!1:e.nodeType===1&&t?!0:n==="array"||n!=="function"&&(t===0||typeof t=="number"&&t>0&&t-1 in e)}function B(e){var t=H[e]={};return b.each(e.match(E)||[],function(e,n){t[n]=!0}),t}function I(e,n,r,i){if(!b.acceptData(e))return;var s,o,u=b.expando,a=typeof n=="string",f=e.nodeType,c=f?
[...remainder of jquery excluded]
[...jquery 的剩余部分排除在外]
Should I not see the test.js file here?
我不应该在这里看到 test.js 文件吗?
Any suggestions as to what the problem might be? Is there any additional info I can provide to help debug this issue? Thanks for any help!
关于问题可能是什么的任何建议?我可以提供任何其他信息来帮助调试此问题吗?谢谢你的帮助!
采纳答案by andersr
The problem appears to have been related to my rails/gem versions. After updating from Rails 3.2.8 to Rails 3.2.14 (which also required updating the rack gem from 1.4.1 to 1.4.5) the issue was resolved and items in assets/javascripts are now being compiled/added to public/assets. Note that I created a completely new gemset as part doing this, so the issue may have had nothing to do with the Rails version and simply was a questions of a "corrupted" gem set that needed to be re-created.
问题似乎与我的 rails/gem 版本有关。从 Rails 3.2.8 更新到 Rails 3.2.14(这也需要将 rack gem 从 1.4.1 更新到 1.4.5)后,问题得到解决,assets/javascripts 中的项目现在正在编译/添加到 public/assets。请注意,我创建了一个全新的 gemset 作为执行此操作的一部分,因此该问题可能与 Rails 版本无关,而只是一个需要重新创建的“损坏的”gem set 的问题。
回答by yfeldblum
Rails will compile all the targets listed in config.assets.precompile. These targets will be compiled to public/assets/. That config item already includes "application.js"and other some targets, so you need to add any othertargets you want to be precompiled to public/assets/. The source file to be compiled for any particular target are automatically computed based on the name of the target; e.g., a target of test.jswould be compiled from app/assets/javascripts/test.jsor from app/assets/javascripts/test.js.coffee, whichever exists.
Rails 将编译config.assets.precompile. 这些目标将被编译为public/assets/. 该配置项已经包含"application.js"和其他一些目标,因此您需要添加要预编译到的任何其他目标public/assets/。为任何特定目标编译的源文件是根据目标名称自动计算的;例如,目标test.js将从app/assets/javascripts/test.js或 从编译app/assets/javascripts/test.js.coffee,以存在者为准。

