Rails 在 openshift 上部署 rails,application.css 无内容

rubybird · 2013年03月06日 · 最后由 Randee 回复于 2013年10月24日 · 4305 次阅读

我根据 rails 的入门教程,写了个简单的 blog,想要部署到 openshift 上,发现 application.css(该 application.css 是 rails 自动创建的) 无法在 production 环境下(准确的说是 openshift 下)生成。

具体情况是这样的: 在 app/assets 下,我有几个文件:

stylesheets/
├── application.css
├── comments.css.scss
├── home.css.scss
├── messages.css.scss
├── pages.css.scss
├── posts.css.scss
└── scaffolds.css.scss

其中,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_tree .
 */

如果我在本地执行

rake assets:precompile

那么可以在 public/assets 目录下找到生成的 css 文件 (在 production 环境下),在本地启动 rails 服务,浏览器访问显示 css 工作正常。

但是,上传到 openshift 后,在浏览器里访问,发现/assets/application.css 的内容为空,而我尝试访问 /assets/home.css 却可以看到有内容。因此,这里可以确认:

在 app/assets/stylesheets 下的 home.scss 已经被正确生成

那么,与 home.scss 同在一个目录下的 application.css 为什么没有被正确生成涅?home.scss 的后缀是 .scss, application.css 的后缀是 css

另外,在 app/assets/javascripts/下的 application.js 却可以正常生成(通过浏览器访问可以看到里面的内容)

openshift 上传时,显示 已经 进行了 rake assets:precompile,

...
remote: Using uglifier (1.3.0) 
remote: Using will_paginate (3.0.4) 
remote: Your bundle is complete! It was installed into ./vendor/bundle
remote: Precompiling with 'bundle exec rake assets:precompile'
remote: [RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
remote: Running .openshift/action_hooks/build
remote: Running .openshift/action_hooks/deploy
remote: Database server found at 127.3.139.1. initializing...
remote: The method 'YAML.enable_arbitrary_object_deserialization!' is deprecated and will be removed in the next release of SafeYAML -- set the SafeYAML::OPTIONS[:default_mode] to either :safe or :unsafe.
remote: [RailsAdmin] RailsAdmin initialization disabled by default. Pass SKIP_RAILS_ADMIN_INITIALIZER=false if you need it.
remote: hot_deploy_added=false
remote: MySQL already running
remote: Done
remote: Running .openshift/action_hooks/post_deploy

各位,有遇到过这样的问题不?或者有什么建议?非常欢迎。

共收到 10 条回复

和我当时一样。不知道你看过这个没有,没看过的话现在可以看看。我认为是没有执行

bundle exec rake assets:precompile

或者关闭

http://guides.ruby-china.org/asset_pipeline.html#1

不确定是否跟 production.rb 有关不,这里也贴一下

cat config/environments/production.rb

RailsApp::Application.configure do
  # Settings specified here will take precedence over those in config/application.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 = true

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

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

  # Generate digests for assets URLs
  config.assets.digest = false

  # Defaults to nil and saved in location specified by config.assets.prefix
  # config.assets.manifest = YOUR_PATH

  # Specifies the header that your server uses for sending files
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # See everything in the log (default is :info)
  # config.log_level = :debug

  # Prepend all log lines with the following tags
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
  # config.assets.precompile += %w( search.js )
  # config.assets.precompile += ["*.js", "*.css"]

  # Disable delivery errors, bad email addresses will be ignored
  # config.action_mailer.raise_delivery_errors = false

  # Enable threaded mode
  # config.threadsafe!

  # 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

  # 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
end

#1 楼 @metal 上传到 openshift 的时候,显示已经执行

remote: Precompiling with 'bundle exec rake assets:precompile'

而且同目录下的 home.scss 已经正确生成 home.css

#3 楼 @rubybird 你有没有试过吧 application.css 改成 application.css.scss

不知道 rails 会不会去识别 css 里这样的东西。

*= require_self
*= require_tree .

看了一下原来 application.css 就是默认。真是搞不懂。我上次也有这样的问题。后来发现没有

bundle exec rake assets:precompile RAILS_ENV= production

#4 楼 @metal

还没有试,刚好有这个想法。

在本地执行 rake assets:precompile 又可以正确生成哦,只是上传到 openshift 的时候没有生成,这是让人困惑的地方。

#6 楼 @rubybird 我看了一下 rails 默认 application.css 就是 css。。。应该没关系。

#5 楼 @metal 确实是没有

bundle exec rake assets:precompile RAILS_ENV= production

的问题

虽然 openshift 的提示了

remote: Precompiling with 'bundle exec rake assets:precompile'

但是似乎它没有正确工作,当我在.openshift/action_hooks/deploy 的文件中增加

bundle exec rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets

重新上传,application.css 工作正常。

还是有人用 openshift 的啊

config/application.rb config.assets.enabled = true ?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册