为什么我的 Rails 资源管道中的 js 文件没有被编译?

2023-12-22

我遇到了与这个问题类似的问题:Rails 资产管道不包括 application.js 清单中所需的文件 https://stackoverflow.com/questions/15328569/rails-asset-pipeline-not-including-required-files-in-application-js-manifest然而,这个问题已经结束了,所以我要重新问它,但要根据我的具体情况。)

环境:

Ruby 2.0.0、Rails 3.2.8、OSX 10.7.5、Chrome 28.0.1500.95

我遇到的主要问题

放置在“/app/assets/javascripts”中的文件似乎没有编译到或出现在“/public/assets”中

一个例子

如果我将一个文件(例如 test.js 文件)放在 app/assets/javascripts 中:

  alert("Hello!");

然后在我的应用程序中重新加载页面,应该会出现此警报。 (我正在复制 Ryan Bates 在他的作品中所做的事情资产管道上的 Railscast http://railscasts.com/episodes/279-understanding-the-asset-pipeline大约 6:30 进入视频)但是,没有出现警报。

调试尝试

我做了一些测试来尝试调试这个问题。

  • (正如下面@Aidan推荐的)如果我“rm -rf public/assets”,那么 将“javascript_include_tag 'test'”添加到我的布局中,警报does出现。不过,也许因为我是n00b,我不知道怎么会这样 帮助我调试问题。
  • 如果我将 //= require_test 添加到 app/javascripts/application.js 中 清单文件,这确实not结果出现警报。

我的清单文件 (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 .

我的 application.rb 文件

 # Enable the asset pipeline
 config.assets.enabled = true

“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"

我的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"

来源自在 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>

查看“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?

[...排除 jquery 的其余部分]

我不应该在这里看到 test.js 文件吗?

关于可能出现的问题有什么建议吗?我可以提供任何其他信息来帮助调试此问题吗?谢谢你的帮助!


该问题似乎与我的 Rails/Gem 版本有关。从Rails 3.2.8更新到Rails 3.2.14(还需要将rack gem从1.4.1更新到1.4.5)后,问题得到解决,资产/javascript中的项目现在正在编译/添加到公共/资产中。请注意,我创建了一个全新的 gemset 作为执行此操作的一部分,因此该问题可能与 Rails 版本无关,而只是需要重新创建的“损坏”gem 集的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的 Rails 资源管道中的 js 文件没有被编译? 的相关文章

随机推荐