【问题标题】:Assets Pipeline资产管道
【发布时间】:2013-02-10 23:00:25
【问题描述】:

我在生产模式下遇到了资产管道问题。我在开发中运行bundle exec rake assets:precompile,它运行良好。但是,当我在生产服务器中运行它时(部署后),无法正确加载 css 和 js 文件。当我通过 url /assets/[file_name] 访问 css 或 js 文件时,出现 500 错误。我认为这是一个权限问题,但/public/assets 下的所有文件都可以访问。 Production.rb 和 development.rb 文件都是完整的(来自脚手架)。不过,我可以看到 html 中列出的所有文件。任何提示将不胜感激。谢谢

生产.rb:

# Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = true

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation can not be found)
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners
  config.active_support.deprecation = :notify

Gemfile(快照):

gem 'rails', '3.2.11'

gem 'pg'
gem 'json'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
#  gem 'therubyracer'

  gem 'uglifier', '>= 1.0.3'
end

gem 'compass-rails'
# Deployment related
gem 'capistrano'
gem 'capistrano-ext'

gem 'jquery-rails', '~> 2.0.0'
gem 'jquery-datatables-rails'

group :development do
  gem 'guard'
end

Application.css:

/*
 * 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 dataTables/jquery.dataTables
 *= require bootstrap.scss
 *= require bootstrap-responsive.scss
 *= require_tree .
 */

我有另一个与 sass 相关的 application.css.scss:

@import "compass";
@import "compass/utilities/tables";

.ftr-ticker {
  background: #333;
  height: 25px;
  width: 100%;
}
....

【问题讨论】:

  • 你能发布你的application.jsapplication.css吗?
  • 我已经用更多信息更新了这个问题:)

标签: ruby-on-rails ruby-on-rails-3 sass asset-pipeline


【解决方案1】:

您修改了清单文件application.css,我认为这会带来问题。我的建议是将您的 scss 文件重命名为其他名称,而不是 application(可能是 custom.css.scss)并将其包含在清单 application.css 文件中,如下所示:

*= require custom

如您所见,样式表文件的名称没有前缀。你的 *= require bootstrap.scss 应该是 *= require bootstrap 。我还建议在所有其他人需要之后移动require_treeThis Railsguide 将帮助您在 Rails 3.2 中使用资产管道。

【讨论】:

  • 您好,感谢您的帮助。我已经尝试过您的解决方案,但无济于事。我想这更像是路径错误,因为我可以在 html 中看到类似 /assets/application-[hash].css 的内容,但无法打开它。该文件确实存在并且有内容。很奇怪吗?实际上,这是我第一次处理资产管道,如果听起来是新手,请见谅
  • 类似哈希的名称是可以的,这些是预编译的资产,我猜你正试图在服务器上获取它们,但你不能。我建议您从您的开发中删除您的public/assets,而不是手动预编译资产,这会扰乱开发过程。 capistrano 任务可以很好地处理预编译。你能说明你把你的css.scss文件放在哪里了吗?
  • 非常感谢 Qumara SixOneTour,我找到了问题。它实际上是在虚拟主机设置中,public/assets 文件夹没有正确代理到后端。不过,我仍然会感谢你的意愿:D
猜你喜欢
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
相关资源
最近更新 更多