资产预编译好,但尝试获取文件时为404

问题描述 投票:9回答:4

好的,所以编译我的资产工作正常,但当我运行时:

thin start -e production

我的javascript或css都没有加载。我的浏览器也取消了获取资产的请求。我不确定为什么会这样,但我怀疑它是因为它认为它是404'。如果您查看顶部图像,您将看到我的application.css文件已编译并存储在我的assets文件夹中,但当我尝试访问该文件时,我收到了我的404.html文件。

是什么赋予了!?

编辑:

我被要求发表我的观点。以下是项目中的一些代码:

<% content_for :title, 'Quick Quote' %>
<% content_for :subtotal, get_subtotal %>
<% content_for :stylesheet_includes do %>
  <%= stylesheet_link_tag "quote", "jquery-ui-timepicker-addon" %>
<% end %>
<% if @quote.errors.any? %>
  <div class="flash alert">
  <% @quote.errors.full_messages.each do |msg| %>
    <div><%= msg %></div>
  <% end %>
  </div>
<% end %>
<h1>Quick Quote</h1>

我的布局:

<!DOCTYPE html>
<html>
<head>
  <title><%= (yield(:title) + " - " unless yield(:title).blank?).to_s + "Online Scheduler Order Taker" %></title>
  <%= stylesheet_link_tag    "application", "jquery-ui-timepicker-addon.css", :media => "all" %>
  <%= yield :stylesheet_includes %>
  <%= javascript_include_tag "application" %>
  <%= yield :javascript_includes %>
  <%= csrf_meta_tags %>
</head>
<body>

我的production.rb中的代码

config.assets.precompile += %w( quote.css jquery-ui-timepicker-addon.css prices.css contact.css )

我的application.css.scss.erb的顶部:

/*
 * 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 . <- commented out
 * require jquery-ui-timepicker-addon.css <- commented out
 */

我的整个application.js文件

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery-ui-timepicker-addon.js
//= require_tree .
ruby-on-rails asset-pipeline
4个回答
7
投票

检查你的config/environments/production.rb文件并添加下一行(如果它还没有):

config.serve_static_assets = true

10
投票

Rails建议默认情况下禁用此设置config.serve_static_assets,即设置为false。以下是rails app中生成的config/environments/production.rb中的默认配置

Disable Rails's static asset server (Apache or nginx will already do this)

config.serve_static_assets = false

因此,如果您在本地应用程序中将其设置为true,那么这仍然没问题。但是,如果您在Apache或ngix或heroku以外的任何其他设备上部署应用程序,则不建议在config.serve_static_assets=true配置文件中生成production.rb。这是Rails指南中的文档。

config.serve_static_files配置Rails本身以提供静态文件。默认为true,但在生产环境中关闭,因为用于运行应用程序的服务器软件(例如NGINX或Apache)应该为静态资产提供服务。与默认设置不同,在运行时(绝对不推荐!)或使用WEBrick在生产模式下测试应用程序时将此设置为true。否则,您将无法使用页面缓存,并且对公共目录下经常存在的文件的请求将无论如何都会打到您的Rails应用程序。

网址 - http://guides.rubyonrails.org/configuring.html


2
投票

你能把这一行放在你当前的环境中吗.rb?

config.serve_static_assets=true

参考:here


1
投票

Rails 5:

config.public_file_server.enabled中的production.rb更改为true或将RAILS_SERVE_STATIC_FILES添加到env变量的任何值。

参考:https://github.com/rails/rails/pull/18100

© www.soinside.com 2019 - 2024. All rights reserved.