rails - 在生产模式下找不到 application.css 资产
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21969549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
rails - application.css asset not found in production mode
提问by Michael Durrant
I'm upgrading an application to use the asset pipeline.
我正在升级应用程序以使用资产管道。
I've got the css assets compiling into an application css file but they not being found when I run the application in production mode with
我已经将 css 资产编译成一个应用程序 css 文件,但是当我在生产模式下运行应用程序时找不到它们
RAILS_ENV=production bundle exec rails s
and I visit any page I get the correct output from the database but no styling and the log shows:
我访问了任何页面,我从数据库中获得了正确的输出,但没有样式,日志显示:
ActionController::RoutingError (No route matches [GET]
"/assets/default.scss-1a27c...f07c.css"):
Even though that file exists in public/assets
即使该文件存在于公共/资产中
$ 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
What do I need to change to get the server to find the asset file?
我需要更改什么才能让服务器找到资产文件?
Same is happening for my other .css files. They get compiled into public/assets with finger prints but then are not found.
我的其他 .css 文件也发生了同样的情况。它们被编译成带有指纹的公共/资产,但随后找不到。
Page source is showing:
页面源显示:
<link href="/assets/default.scss-1a27c...f07c.css"
media="screen" rel="stylesheet" type="text/css" />
The rails (haml) source is = stylesheet_link_tag 'default.scss.css'
rails (haml) 来源是 = stylesheet_link_tag 'default.scss.css'
public.assets
curently includes has the following files.
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 has
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
has:
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
This seems to be happening for all assets, e.g.
这似乎发生在所有资产上,例如
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"):
回答by awendt
Rails by default doesn't serve assets under public
. See your production.rb
:
默认情况下,Rails 不提供public
. 看到你的production.rb
:
config.serve_static_assets = true
Change that to true and you're good to go. (Note: you don't want that to be true
in production, remember to change it back before deploying!)
将其更改为 true,您就可以开始了。(注意:你不希望它true
在生产中,记得在部署前改回来!)
See Configuring Rails Applicationsfor details.
有关详细信息,请参阅配置 Rails 应用程序。
In rails 6, in the default production.rb
there should be a line
在 rails 6中,默认情况下production.rb
应该有一条线
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
So run your server with
所以运行你的服务器
RAILS_SERVE_STATIC_FILES=true rails server -e production
or set config.public_file_server.enabled=true
in production.rb
. See answers below for rails 4 and 5.
或设置config.public_file_server.enabled=true
在production.rb
. 请参阅下面有关轨道 4 和 5 的答案。
回答by Ben Amos
The Rails 5 solution is similar to the Rails 4 solutiongiven by Jules Copeland above.
Rails 5 解决方案类似于上面 Jules Copeland 给出的Rails 4 解决方案。
In your pre-generated config/environments/production.rb
file, there should be an entry that looks something like this:
在您预先生成的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?
I found a decent explanation for this setting in the Configuring Rails Applications guideat http://guides.rubyonrails.org:
我在http://guides.rubyonrails.org的配置 Rails 应用程序指南中找到了对此设置的体面解释:
config.public_file_server.enabled configures Rails to serve static files from the public directory. This option defaults to true, but in the production environment it is set to false because the server software (e.g. NGINX or Apache) used to run the application should serve static files instead. If you are running or testing your app in production mode using WEBrick (it is not recommended to use WEBrick in production) set the option to true. Otherwise, you won't be able to use page caching and request for files that exist under the public directory.
config.public_file_server.enabled 将 Rails 配置为从公共目录提供静态文件。此选项默认为 true,但在生产环境中设置为 false,因为用于运行应用程序的服务器软件(例如 NGINX 或 Apache)应改为提供静态文件。如果您使用 WEBrick 在生产模式下运行或测试您的应用程序(不建议在生产中使用 WEBrick),请将选项设置为 true。否则,您将无法使用页面缓存和请求公共目录下存在的文件。
Conclusion: In production, starting your rails server with RAILS_SERVE_STATIC_FILES=1
will allow Rails to serve any files in the public/assets directory just as a web server would. Keep in mind, Rails is an app server and will not do this as efficiently as a web server (e.g. NGINX, Apache, etc.). For real-world applications, you should have a dedicated web server sitting in front of Rails which will serve static assets by itself and only bother Rails for dynamic content as needed. For more details, see this articleby Justin Weiss on the differences between web servers and app servers.
结论:在生产环境中,启动 Rails 服务器RAILS_SERVE_STATIC_FILES=1
将允许 Rails 为 public/assets 目录中的任何文件提供服务,就像 Web 服务器一样。请记住,Rails 是一个应用程序服务器,不会像 Web 服务器(例如 NGINX、Apache 等)那样有效地执行此操作。对于实际应用程序,您应该在 Rails 前面有一个专用的 Web 服务器,它会自行提供静态资产,并且只在需要时为动态内容打扰 Rails。有关更多详细信息,请参阅Justin Weiss 撰写的关于 Web 服务器和应用程序服务器之间差异的文章。
回答by Jules Copeland
In Rails 4, you can get them to show in production (running locally), by passing an environment variable:
在 Rails 4 中,您可以通过传递环境变量来让它们显示在生产环境中(在本地运行):
RAILS_SERVE_STATIC_FILES=true rails server -e production
RAILS_SERVE_STATIC_FILES=true rails server -e production
This should work as long as you have this line in /config/environments/production.rb
:
只要你有这一行,这应该工作/config/environments/production.rb
:
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
回答by Lenin Raj Rajasekaran
When you do rake assets:precompile
, your assets go into public directory. See if you can find those files in public/assets/
当你这样做时rake assets:precompile
,你的资产进入公共目录。看看你能不能找到这些文件public/assets/
You should see something like this:
您应该会看到如下内容:
I, [2014-02-23T20:06:21.853314 #26915] INFO -- : Writing app_root/public/assets/application-ecd8636fc80ea2b712039c4abc365da9.css