我正在升级应用程序以使用资产管道。
我已将css资源编译到应用程序css文件中,但是当我在生产模式下使用
运行应用程序时找不到它们RAILS_ENV=production bundle exec rails s
我访问任何页面,我从数据库中获得正确的输出,但没有样式和日志显示:
ActionController::RoutingError (No route matches [GET]
"/assets/default.scss-1a27c...f07c.css"):
即使该文件存在于公共/资产
中$ ls public/assets/def*
public/assets/default.scss-1a27c...f07c.css public/assets/default.scss.css
public/assets/default.scss-1a27c...f07c.css.gz public/assets/default.scss.css.gz
我需要更改什么才能让服务器找到资产文件?
我的其他.css文件也是如此。他们用指纹编译成公共/资产,但后来找不到。
页面来源显示:
<link href="/assets/default.scss-1a27c...f07c.css"
media="screen" rel="stylesheet" type="text/css" />
rails(haml)源是= stylesheet_link_tag 'default.scss.css'
public.assets
包含以下文件。
$ ls public/assets/def*
public/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css
public/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css.gz
public/assets/default.scss.css
public/assets/default.scss.css.gz
application.rb有
$ cat config/application.rb
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Linker
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.assets.enabled = true
config.assets.initialize_on_precompile = false # For Heroku
config.assets.version = '1.0'
end
end
config/environments/production
有:
$ cat config/environments/production.rb
Linker::Application.configure do
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.assets.precompile += ['default.scss.css','main.css', 'jquery-ui-1.8.22.custom.css']
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
这似乎适用于所有资产,例如
Started GET "/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/default.scss-1a27c22229b7b522066181f27af4f07c.css"):
Started GET "/assets/main-6864687b4114a1c316e444bd90f233ff.css" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/main-6864687b4114a1c316e444bd90f233ff.css"):
Started GET "/assets/jquery-ui-1.8.22.custom-24319b4b1218846a3fe22a0479ae98b4.css" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/jquery-ui-1.8.22.custom-24319b4b1218846a3fe22a0479ae98b4.css"):
Started GET "/assets/application-fc1d492d730f2a45581a40eac4607db8.js" for 127.0.0.1 at 2014-02-23 10:24:47 -0500
ActionController::RoutingError (No route matches [GET] "/assets/application-fc1d492d730f2a45581a40eac4607db8.js"):
Started GET "/images/link.ico" for 127.0.0.1 at 2014-02-23 10:24:48 -0500
ActionController::RoutingError (No route matches [GET] "/images/link.ico"):
答案 0 :(得分:29)
默认情况下,Rails不会为public
下的资产提供服务。查看您的production.rb
:
config.serve_static_assets = true
将其更改为true,您就可以了。 (注意:您不希望它在生产中true
,请记得在部署之前将其更改回来!)
有关详细信息,请参阅Configuring Rails Applications。
答案 1 :(得分:9)
在Rails 4中,您可以通过传递环境变量让它们在生产中显示(在本地运行):
RAILS_SERVE_STATIC_FILES=true rails server -e production
只要您在/config/environments/production.rb
:
config.serve_static_files = ENV [&#39; RAILS_SERVE_STATIC_FILES&#39;]。现在?
答案 2 :(得分:8)
Rails 5解决方案类似于上面Jules Copeland提供的Rails 4 solution。
在预先生成的config/environments/production.rb
文件中,应该有一个看起来像这样的条目:
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
我在Configuring Rails Applications guide http://guides.rubyonrails.org处找到了关于此设置的合适解释:
config.public_file_server.enabled将Rails配置为静态服务 公共目录中的文件。此选项默认为true,但在 生产环境因服务器而设置为false 用于运行应用程序的软件(例如NGINX或Apache)应该 改为提供静态文件。如果您正在运行或测试您的应用程序 使用WEBrick的生产模式(不建议使用WEBrick production)将选项设置为true。否则,你将无法做到 使用页面缓存和请求公共存在的文件 。目录
结论:在生产中,使用RAILS_SERVE_STATIC_FILES=1
启动rails服务器将允许Rails像web服务器一样提供public / assets目录中的任何文件。请记住,Rails是一个应用服务器,不会像Web服务器那样有效(例如NGINX,Apache等)。对于真实世界的应用程序,您应该在Rails前面有一个专用的Web服务器,它将自行提供静态资产,并且只根据需要为动态内容打扰Rails。有关详细信息,请参阅Justin Weiss关于Web服务器和应用服务器之间差异的this article。
答案 3 :(得分:4)
执行rake assets:precompile
时,您的资产会进入公共目录。看看您是否可以在public/assets/
你应该看到这样的事情:
I, [2014-02-23T20:06:21.853314 #26915] INFO -- : Writing app_root/public/assets/application-ecd8636fc80ea2b712039c4abc365da9.css