Asset Precompilation OK, but 404 when trying to ge

2020-08-23 01:37发布

问题:

Ok so compiling my assets is working fine but when I run:

thin start -e production

none of my javascript or css is loading. My browser is also cancelling the requests to get my assets. I'm not sure why this is but I suspect its because it thinks its 404'ing on them. If you look at the top image you'll see that my application.css file was compiled and stored in my assets folder but when I try to access the file, I'm getting my 404.html file.

What gives!?

Edit:

I was asked to post my view. Here is some of the code in the project:

<% 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>

my layout:

<!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>

code in my production.rb

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

Top of my 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
 */

my entire application.js file

// 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 .

回答1:

Check your config/environments/production.rb file and add next line to it (if it does not have it yet):

config.serve_static_assets = true


回答2:

Rails recommends that this setting config.serve_static_assets by default should be disabled i.e. set to false. Here is the default configuration in config/environments/production.rb generated in rails app

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

config.serve_static_assets = false

So if you are setting it to true in your local app then that's still fine. But if you are deploying your app on Apache or ngix or anything other than heroku then its not advisable to make config.serve_static_assets=true in your production.rb config file. Here is the documentation in Rails guides.

config.serve_static_files configures Rails itself to serve static files. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.

URL - http://guides.rubyonrails.org/configuring.html



回答3:

Can you put this line in your current environment.rb?

config.serve_static_assets=true

Reference: here



回答4:

Rails 5:

Change config.public_file_server.enabled in production.rb to true or add RAILS_SERVE_STATIC_FILES with any value to env variables.

Reference: https://github.com/rails/rails/pull/18100