2014-02-23 48 views
12

我在升級應用程序以使用資產管道。rails - 在生產模式下找不到application.css資產

我已經得到了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" /> 

導軌(HAML)源是= stylesheet_link_tag 'default.scss.css'

public.assets curently包括具有以下文件。

$ 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"): 
+1

您是否做'rake assets:precompile' – emaillenin

+0

是的,我做了'$ RAILS_ENV =生產包exec rake assets:precompile'並重新啓動了我的服務器 –

+1

你能複製你的application.rb和production.rb文件嗎? – arieljuod

回答

22

Rails的默認情況下不提供public下的資產。查看你的production.rb

config.serve_static_assets = false 

把它變成真,你就可以走了。 (注意:您不希望在生產中成爲true,請記住在部署之前將其更改回去)

有關詳細信息,請參見Configuring Rails Applications

+0

爲什麼它在生產中是錯誤的?我正在使用rails_12factor和heroku。感謝您的進一步解釋! –

+1

@LeoBrown Rails假定在生產中,您有一個在Rails之前運行的Web服務器(Apache,nginx),或者甚至是專門用於靜態資產的CDN。其中任何一種都可以比Rails更快地提供靜態資產。 – awendt

+3

對於Rails 5:在config/environments/production.rb中將'config.public_file_server.enabled'設置爲'true'。 – vmarquet

3

當你做rake assets:precompile,你的資產進入公共目錄。看看你是否能在public/assets/

找到這些文件,你應該看到這樣的事情:

I, [2014-02-23T20:06:21.853314 #26915] INFO -- : Writing app_root/public/assets/application-ecd8636fc80ea2b712039c4abc365da9.css 
+1

當你預編譯,它寫入哪個目錄? – emaillenin

+0

+1謝謝,它似乎是寫給公衆/資產,至少這是指紋文件的位置。 –

+1

新寫入的文件名和被請求的文件名是否匹配? – emaillenin

9

在Rails 4,你可以讓他們在生產中表現出通過將環境變量(本地運行):

RAILS_SERVE_STATIC_FILES=true rails server -e production

這應該只要你有這條線工作,/config/environments/production.rb

config.serve_static_files = ENV ['RAILS_SERVE_STATIC_FILES']。present?

5

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 guidehttp://guides.rubyonrails.org

config.public_file_server.enabled將Rails配置爲從公共目錄提供靜態 文件。此選項默認爲true,但在 生產環境中,它被設置爲false,因爲用於運行應用程序的服務器 軟件(例如NGINX或Apache)應該替代地爲 服務靜態文件。如果您使用WEBrick在 生產模式下運行或測試您的應用(不建議使用 生產中的WEBrick),請將該選項設置爲true。否則,您將無法使用 頁面緩存和請求存在於公共 目錄下的文件。

結論:在生產中,開始你的鋼軌服務器將使Rails的服務於大衆/資產目錄中的任何文件,就如同將一個Web服務器。請記住,Rails是一款應用服務器,不會像Web服務器那樣高效(例如NGINX,Apache等)。對於真實世界的應用程序,您應該有一個專門的Web服務器,它位於Rails前面,它將自己提供靜態資源,並只根據需要爲動態內容打擾Rails。有關更多詳細信息,請參閱Justin Weiss的this article關於Web服務器和應用程序服務器之間的差異。