【发布时间】:2013-03-11 22:29:45
【问题描述】:
我已经为此苦苦挣扎了几天 - 我在我的 rails 应用程序中添加了引导程序(和其他一些 gem),但由于某种原因,适当的 CSS 文件没有呈现,因此引导程序样式没有显示.
以下是我文件的相关部分:
application.html.erb in app/views/layouts:
<head>
<title>Myapp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-dropdown.js"></script>
</head>
application.css in app/assets/stylesheets:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require_tree .
*/
(这个文件夹里还有很多CSS文件,包括bootstrap_and_overrides.css和gmaps4rails.css)。
以及我加载页面时的服务器日志:
$ rails server
=> Booting WEBrick
=> Rails 3.2.8 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.
Called from: /Users/me/.rvm/gems/ruby-2.0.0-p0/gems/actionpack-3.2.8/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `initialize'.
[2013-03-11 15:19:32] INFO WEBrick 1.3.1
[2013-03-11 15:19:32] INFO ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2]
[2013-03-11 15:19:32] INFO WEBrick::HTTPServer#start: pid=94815 port=3000
Started GET "/" for 127.0.0.1 at 2013-03-11 15:19:35 -0700
Processing by HomeController#index as HTML
Rendered home/index.html.erb within layouts/application (2.8ms)
Trip Load (0.2ms) SELECT "trips".* FROM "trips"
Rendered layouts/_navigation.html.erb (62.0ms)
Completed 200 OK in 143ms (Views: 141.0ms | ActiveRecord: 1.3ms)
以及实际在 Chrome 中呈现的源代码:
<head>
<title>Myapp</title>
<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="GfG+KCsrNZaChqWvUz/g5RqsdHrXOtumFzHaOjJmFQg=" name="csrf-token" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://raw.github.com/twitter/bootstrap/master/js/bootstrap-dropdown.js"></script>
</head>
还有我的Gemfile:
gem "bootstrap-sass", '~> 2.3.0.1'
gem "simple_form"
gem 'gmaps4rails'
还有config/environments/development.rb:
config.cache_classes = false
# Log error messages when you accidentally call methods on nil.
config.whiny_nils = true
# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
# Only use best-standards-support built into browsers
config.action_dispatch.best_standards_support = :builtin
# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
# Log the query plan for queries taking more than this (works
# with SQLite, MySQL, and PostgreSQL)
config.active_record.auto_explain_threshold_in_seconds = 0.5
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
我将 config/application.rb 中的资产管道设置为 true,并且我没有更改调试设置。我的RAILS_ENV 是development。据我了解,由于require_tree . 调用,rails 应该编译和提供所有 CSS 文件,包括 boostrap_and_overrides.css.scss。我已经多次运行rake assets:precompile 和bundle exec rake assets:precompile,但我认为这无关紧要。我正在运行 ruby 2.0.0p0(2013-02-24 修订版 39474)。
真正奇怪的是,我和我在这个项目上的合作伙伴拥有完全相同的代码(来自我们的 github 存储库),它可以在他们的本地机器上运行,但它不能在我的机器上运行。有人对正在发生的事情有任何意见吗?
【问题讨论】:
-
正确,你不需要在开发中预编译资产(事实上,你可能想删除 /public/assets 中的所有文件)。你能从 Chrome 的源视图中看到 css 的内容吗?在 css 链接在哪里之前我也遇到过类似的问题,但它们都是空的。
-
不,在 Chrome 中呈现的 CSS 文件是空的(这很奇怪,因为它至少应该包含
requirecmets 等)。对于它的价值,manifest.yml中指定的public/assets中的 MD5 文件也是空的(但这在开发中应该不重要,对吧?)我突然想到,事情可能对我团队中的其他人都有效但不适合我,因为他们可能有RAILS_ENV=production,但我也尝试将我的设置为生产,但没有配置任何服务资产,所以我认为这不是问题。 -
你能把你的 development.rb 贴在 config/environments 中吗(你说它和其他的没有什么不同?)。我在生产中遇到过这个错误,但这是一个错误的独角兽设置(不是 rails 问题)。这肯定是您当地的问题。
-
您可能希望在开发模式下删除公共/资产(或至少该目录中的所有文件),否则 Rails 服务器将提供这些文件,而不是即时编译文件。我会删除这些文件并重新启动 Rails 服务器
-
当然,我刚刚用它更新了帖子。我没有更改那个文件,无论如何这都无关紧要,因为我团队中的每个人都有相同的文件(因为我们将它推送到 github),对吧?
标签: ruby-on-rails asset-pipeline twitter-bootstrap-rails