2011-12-19 65 views
10

EDIT 4,5和,任何更多的想法6application.css不擔任資產

8小時歡迎:)也許這個錯誤是已知的,解決了,但我得到了我的行爲在編輯2 & 3,當您在應用程序/資產/樣式表一個.css.erb文件有這樣的描述:

li { 
    background-image: url(<%= asset_path "logo_80px_80px.png" %>); 
} 

問題的部分似乎是asset_path方法調用.css.scss還可以,但休息它與:

li { 
    background-image: url(image-path("logo_80px_80px.png")); 
} 

我的Gemfile是:

source 'http://rubygems.org' 

gem 'rails', '3.1.3' 
gem 'pg', '0.11.0' 
gem 'gravatar_image_tag', '1.0.0.pre2' 
gem 'will_paginate' 
gem 'rake' 
gem 'jquery-rails' 
gem 'nested_form' 
gem 'acts-as-taggable-on' 

gem 'sass-rails' 
group :assets do 
    gem 'coffee-rails', '~> 3.1.0' 
    gem 'uglifier', '>= 1.0.3' 
end 

group :development do 
    gem 'rspec-rails', '2.5.0' 
    gem 'annotate', '2.4.0' 
    gem 'faker', '0.3.1' 
end 


group :test do 
    gem 'rspec', '2.5.0' 
    gem 'webrat', '0.7.1' 
    gem 'factory_girl_rails', '1.0' 
end 

group :production do 
    gem 'therubyracer-heroku' 
end 

我Gemfile.lock的是:

GEM 
    remote: http://rubygems.org/ 
    specs: 
    actionmailer (3.1.3) 
     actionpack (= 3.1.3) 
     mail (~> 2.3.0) 
    actionpack (3.1.3) 
     activemodel (= 3.1.3) 
     activesupport (= 3.1.3) 
     builder (~> 3.0.0) 
     erubis (~> 2.7.0) 
     i18n (~> 0.6) 
     rack (~> 1.3.5) 
     rack-cache (~> 1.1) 
     rack-mount (~> 0.8.2) 
     rack-test (~> 0.6.1) 
     sprockets (~> 2.0.3) 
    activemodel (3.1.3) 
     activesupport (= 3.1.3) 
     builder (~> 3.0.0) 
     i18n (~> 0.6) 
    activerecord (3.1.3) 
     activemodel (= 3.1.3) 
     activesupport (= 3.1.3) 
     arel (~> 2.2.1) 
     tzinfo (~> 0.3.29) 
    activeresource (3.1.3) 
     activemodel (= 3.1.3) 
     activesupport (= 3.1.3) 
    activesupport (3.1.3) 
     multi_json (~> 1.0) 
    acts-as-taggable-on (2.2.1) 
     rails (~> 3.1) 
    annotate (2.4.0) 
    arel (2.2.1) 
    builder (3.0.0) 
    coffee-rails (3.1.1) 
     coffee-script (>= 2.2.0) 
     railties (~> 3.1.0) 
    coffee-script (2.2.0) 
     coffee-script-source 
     execjs 
    coffee-script-source (1.1.3) 
    diff-lcs (1.1.3) 
    erubis (2.7.0) 
    execjs (1.2.12) 
     multi_json (~> 1.0) 
    factory_girl (1.3.3) 
    factory_girl_rails (1.0) 
     factory_girl (~> 1.3) 
     rails (>= 3.0.0.beta4) 
    faker (0.3.1) 
    gravatar_image_tag (1.0.0.pre2) 
    hike (1.2.1) 
    i18n (0.6.0) 
    jquery-rails (1.0.19) 
     railties (~> 3.0) 
     thor (~> 0.14) 
    json (1.6.3) 
    mail (2.3.0) 
     i18n (>= 0.4.0) 
     mime-types (~> 1.16) 
     treetop (~> 1.4.8) 
    mime-types (1.17.2) 
    multi_json (1.0.4) 
    nested_form (0.1.1) 
    nokogiri (1.5.0-x86-mingw32) 
    pg (0.11.0-x86-mingw32) 
    polyglot (0.3.3) 
    rack (1.3.5) 
    rack-cache (1.1) 
     rack (>= 0.4) 
    rack-mount (0.8.3) 
     rack (>= 1.0.0) 
    rack-ssl (1.3.2) 
     rack 
    rack-test (0.6.1) 
     rack (>= 1.0) 
    rails (3.1.3) 
     actionmailer (= 3.1.3) 
     actionpack (= 3.1.3) 
     activerecord (= 3.1.3) 
     activeresource (= 3.1.3) 
     activesupport (= 3.1.3) 
     bundler (~> 1.0) 
     railties (= 3.1.3) 
    railties (3.1.3) 
     actionpack (= 3.1.3) 
     activesupport (= 3.1.3) 
     rack-ssl (~> 1.3.2) 
     rake (>= 0.8.7) 
     rdoc (~> 3.4) 
     thor (~> 0.14.6) 
    rake (0.9.2.2) 
    rdoc (3.12) 
     json (~> 1.4) 
    rspec (2.5.0) 
     rspec-core (~> 2.5.0) 
     rspec-expectations (~> 2.5.0) 
     rspec-mocks (~> 2.5.0) 
    rspec-core (2.5.2) 
    rspec-expectations (2.5.0) 
     diff-lcs (~> 1.1.2) 
    rspec-mocks (2.5.0) 
    rspec-rails (2.5.0) 
     actionpack (~> 3.0) 
     activesupport (~> 3.0) 
     railties (~> 3.0) 
     rspec (~> 2.5.0) 
    sass (3.1.12) 
    sass-rails (3.1.5) 
     actionpack (~> 3.1.0) 
     railties (~> 3.1.0) 
     sass (~> 3.1.10) 
     tilt (~> 1.3.2) 
    sprockets (2.0.3) 
     hike (~> 1.2) 
     rack (~> 1.0) 
     tilt (~> 1.1, != 1.3.0) 
    therubyracer-heroku (0.8.1.pre3) 
    thor (0.14.6) 
    tilt (1.3.3) 
    treetop (1.4.10) 
     polyglot 
     polyglot (>= 0.3.1) 
    tzinfo (0.3.31) 
    uglifier (1.2.0) 
     execjs (>= 0.3.0) 
     multi_json (>= 1.0.2) 
    webrat (0.7.1) 
     nokogiri (>= 1.2.0) 
     rack (>= 1.0) 
     rack-test (>= 0.5.3) 
    will_paginate (3.0.2) 

PLATFORMS 
    x86-mingw32 

DEPENDENCIES 
    acts-as-taggable-on 
    annotate (= 2.4.0) 
    coffee-rails (~> 3.1.0) 
    factory_girl_rails (= 1.0) 
    faker (= 0.3.1) 
    gravatar_image_tag (= 1.0.0.pre2) 
    jquery-rails 
    nested_form 
    pg (= 0.11.0) 
    rails (= 3.1.3) 
    rake 
    rspec (= 2.5.0) 
    rspec-rails (= 2.5.0) 
    sass-rails 
    therubyracer-heroku 
    uglifier (>= 1.0.3) 
    webrat (= 0.7.1) 
    will_paginate 

application.rb中的內容:

require File.expand_path('../boot', __FILE__) 

require 'rails/all' 


if defined?(Bundler) 
    Bundler.require(*Rails.groups(:assets => %w(development test))) 
end 

module AjpApp 
    class Application < Rails::Application 
    config.encoding = "utf-8" 
    config.filter_parameters += [:password] 
    config.assets.enabled = true 
    config.assets.version = '1.0' 
    end 
end 

配置/環境/ production.rb內容:

AjpApp::Application.configure do 
    config.cache_classes = true 
    config.consider_all_requests_local  = false 
    config.action_controller.perform_caching = true 
    config.serve_static_assets = false 
    config.assets.compress = true 
    config.assets.compile = true 
    config.assets.digest = true 
    config.i18n.fallbacks = true 
    config.active_support.deprecation = :notify 
end 

---存檔---

原帖

我有一個服務於application.css文件在生產模式下,當一個Rails 3.1的應用程序。 After having some unrelated problems我發現了precompiling the assets使用:

bundle exec rake assets:precompile RAILS_ENV=production 

我application.css文件,預編譯的產生:「應用程序79d6fafe46c10758d8f5a921f1e55ecd.css」,但現在沒有,儘管存在於公共/ assets文件夾服務。 application.js文件服務正常(如application-9a36deba94d6308ab9bebe4e30a93959.js)。另外我注意到,在創建.css.erb文件後,第一次出現此問題,正如precompilation上的文章所建議的。

對我想做什麼有任何想法?我將嘗試撤消.css.erb文件,再次運行預編譯,並查看是否獲得了我的application.css文件。

編輯1

問題並非來自改變.css文件.css.erb。而且erb工作正常。問題似乎都在使用asset_path功能例如爲:

.button_one { 
    background-image: url(<%= asset_path "activities/text_editor_toolbar.png" %>); 
} 

這個工程給予正確的字符串例如爲:assets/activities/text_editor_toolbar.png",這在編譯application-(hash).css文件,該文件是在發展,但不是生產模式提供顯示出來。這太奇怪了。 :| :@

編輯2 好吧,這只是變得更好,但更奇怪。似乎如果我在生產模式下運行它,它會失敗。在生產和it'll spend ages and launch the 'Microsoft Console Based Script Host' just like it's precompiling the assets,從輸出好像它在做什麼,最後呈現頁面和服務的應用程序 - (散)之前在發展再次運行它,然後再次成功的CSS:

C:\documents\ror\ajp_app>rails s -e production 
=> Booting WEBrick 
=> Rails 3.1.3 application starting in production on http://0.0.0.0:3000 
=> Call with -d to detach 
=> Ctrl-C to shutdown server 
[2011-12-19 23:59:12] INFO WEBrick 1.3.1 
[2011-12-19 23:59:12] INFO ruby 1.9.2 (2011-07-09) [i386-mingw32] 
[2011-12-19 23:59:12] INFO WEBrick::HTTPServer#start: pid=4052 port=3000 
cache: [GET /] miss 


Started GET "/" for 127.0.0.1 at 2011-12-19 23:59:26 +0000 
    Processing by PagesController#home as HTML 
Rendered pages/home.html.erb within layouts/application (85.0ms) 
Rendered layouts/_header.html.erb (2.0ms) 
Rendered layouts/_footer.html.erb (1.0ms) 
Completed 200 OK in 393ms (Views: 382.0ms | ActiveRecord: 10.0ms) 
cache: [GET /assets/application-a05ca896e645eacb50266c7866d18a0f.css] miss 


Started GET "/assets/application-a05ca896e645eacb50266c7866d18a0f.css" for 127.0.0.1 at 2011-12-19 23:59:27 +0000 
Nonexistent asset application-a05ca896e645eacb50266c7866d18a0f.css @ a05ca896e645eacb50266c7866d18a0f 
Served asset /application-a05ca896e645eacb50266c7866d18a0f.css - 404 Not Found (44ms) 

ActionController::RoutingError (No route matches [GET] "/assets/application-a05ca896e645eacb50266c7866d18a0f.css"): 


Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout 
(1.0ms) 
[2011-12-19 23:59:39] INFO going to shutdown ... 
[2011-12-19 23:59:39] INFO WEBrick::HTTPServer#start done. 
Exiting 

C:\documents\ajp_app>rails s 
=> Booting WEBrick 
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000 
=> Call with -d to detach 
=> Ctrl-C to shutdown server 
[2011-12-20 00:00:14] INFO WEBrick 1.3.1 
[2011-12-20 00:00:14] INFO ruby 1.9.2 (2011-07-09) [i386-mingw32] 
[2011-12-20 00:00:14] INFO WEBrick::HTTPServer#start: pid=2752 port=3000 


Started GET "/" for 127.0.0.1 at 2011-12-20 00:00:20 +0000 
    Processing by PagesController#home as HTML 
    ←[1m←[36m (0.0ms)←[0m ←[1mSHOW search_path←[0m 
    ←[1m←[35mUser Load (1.0ms)←[0m SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
Rendered pages/home.html.erb within layouts/application (171.0ms) 
Compiled application.css (4ms) (pid 2752) 
Compiled colours.css (9ms) (pid 2752) 
Compiled custom.css (1ms) (pid 2752) 
Compiled activity.css (26ms) (pid 2752) 
Compiled jquery/jquery-ui-1.8.13.custom.css (1ms) (pid 2752) 
Compiled ellib/elcommon.css (30ms) (pid 2752) 
Compiled ellib/eldialogform.css (0ms) (pid 2752) 
Compiled elrte/elrte.css (0ms) (pid 2752) 
Compiled activities/text_editor.css (2ms) (pid 2752) 
Compiled errors.activities.css (0ms) (pid 2752) 
Compiled errors_and_notices.css (0ms) (pid 2752) 
Compiled general.css (0ms) (pid 2752) 
Compiled application.js (15ms) (pid 2752) 
Compiled jquery.js (13ms) (pid 2752) 
Compiled jquery_ujs.js (0ms) (pid 2752) 
Compiled jquery-ui.js (19ms) (pid 2752) 
Compiled AJP_main.js (1ms) (pid 2752) 
Compiled ajp.utilities.js (2ms) (pid 2752) 
Rendered layouts/_header.html.erb (4.0ms) 
Rendered layouts/_footer.html.erb (2.0ms) 
Completed 200 OK in 1091ms (Views: 1076.0ms | ActiveRecord: 15.0ms) 


Started GET "/assets/custom.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /custom.css - 304 Not Modified (6ms) 


Started GET "/assets/jquery/jquery-ui-1.8.13.custom.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /jquery/jquery-ui-1.8.13.custom.css - 304 Not Modified (7ms) 


Started GET "/assets/ellib/eldialogform.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /ellib/eldialogform.css - 304 Not Modified (5ms) 


Started GET "/assets/elrte/elrte.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /elrte/elrte.css - 304 Not Modified (5ms) 


Started GET "/assets/activities/text_editor.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /activities/text_editor.css - 304 Not Modified (9ms) 


Started GET "/assets/activity.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /activity.css - 200 OK (10ms) 


Started GET "/assets/errors.activities.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /errors.activities.css - 304 Not Modified (3ms) 


Started GET "/assets/errors_and_notices.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /errors_and_notices.css - 304 Not Modified (3ms) 


Started GET "/assets/general.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /general.css - 304 Not Modified (3ms) 


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /jquery.js - 304 Not Modified (12ms) 


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2011-12-20 00:00:28 +0000 
Served asset /jquery_ujs.js - 304 Not Modified (8ms) 


Started GET "/assets/jquery-ui.js?body=1" for 127.0.0.1 at 2011-12-20 00:00:29 +0000 
Served asset /jquery-ui.js - 304 Not Modified (42ms) 


Started GET "/assets/AJP_main.js?body=1" for 127.0.0.1 at 2011-12-20 00:00:29 +0000 
Served asset /AJP_main.js - 304 Not Modified (3ms) 


Started GET "/assets/ajp.utilities.js?body=1" for 127.0.0.1 at 2011-12-20 00:00:29 +0000 
Served asset /ajp.utilities.js - 304 Not Modified (10ms) 


Started GET "/assets/ellib/elcommon.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:29 +0000 
Served asset /ellib/elcommon.css - 304 Not Modified (5ms) 


Started GET "/assets/colours.css?body=1" for 127.0.0.1 at 2011-12-20 00:00:29 +0000 
Served asset /colours.css - 304 Not Modified (12ms) 


Started GET "/assets/activities/hello.png" for 127.0.0.1 at 2011-12-20 00:00:30 +0000 
Served asset /activities/hello.png - 404 Not Found (13ms) 

ActionController::RoutingError (No route matches [GET] "/assets/activities/hello.png"): 


Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout 
(1.0ms) 
[2011-12-20 00:00:37] INFO going to shutdown ... 
[2011-12-20 00:00:37] INFO WEBrick::HTTPServer#start done. 
Exiting 

C:\documents\ajp_app>rails s -e production 
=> Booting WEBrick 
=> Rails 3.1.3 application starting in production on http://0.0.0.0:3000 
=> Call with -d to detach 
=> Ctrl-C to shutdown server 
[2011-12-20 00:01:17] INFO WEBrick 1.3.1 
[2011-12-20 00:01:17] INFO ruby 1.9.2 (2011-07-09) [i386-mingw32] 
[2011-12-20 00:01:17] INFO WEBrick::HTTPServer#start: pid=5912 port=3000 
cache: [GET /] miss 


Started GET "/" for 127.0.0.1 at 2011-12-20 00:01:20 +0000 
    Processing by PagesController#home as HTML 
Rendered pages/home.html.erb within layouts/application (91.0ms) 
Rendered layouts/_header.html.erb (3.0ms) 
Rendered layouts/_footer.html.erb (1.0ms) 
Completed 200 OK in 400ms (Views: 387.0ms | ActiveRecord: 12.0ms) 
cache: [GET /assets/application-a05ca896e645eacb50266c7866d18a0f.css] miss, store 


Started GET "/assets/application-a05ca896e645eacb50266c7866d18a0f.css" for 127.0.0.1 at 2011-12-20 00:01:21 +0000 
Compiled application-a05ca896e645eacb50266c7866d18a0f.css (4ms) (pid 5912) 
Compiled colours.css (9ms) (pid 5912) 
Compiled custom.css (0ms) (pid 5912) 
Compiled activity.css (21ms) (pid 5912) 
Compiled jquery/jquery-ui-1.8.13.custom.css (1ms) (pid 5912) 
Compiled ellib/elcommon.css (0ms) (pid 5912) 
Compiled ellib/eldialogform.css (1ms) (pid 5912) 
Compiled elrte/elrte.css (1ms) (pid 5912) 
Compiled activities/text_editor.css (0ms) (pid 5912) 
Compiled errors.activities.css (1ms) (pid 5912) 
Compiled errors_and_notices.css (0ms) (pid 5912) 
Compiled general.css (0ms) (pid 5912) 
Served asset /application-a05ca896e645eacb50266c7866d18a0f.css - 200 OK (871ms) 
cache: [GET /assets/application-9a36deba94d6308ab9bebe4e30a93959.js] miss 


Started GET "/assets/application-9a36deba94d6308ab9bebe4e30a93959.js" for 127.0.0.1 at 2011-12-20 00:01:22 +0000 
Compiled application-9a36deba94d6308ab9bebe4e30a93959.js (9ms) (pid 5912) 
Compiled jquery.js (13ms) (pid 5912) 
Compiled jquery_ujs.js (1ms) (pid 5912) 
Compiled jquery-ui.js (19ms) (pid 5912) 
Compiled AJP_main.js (0ms) (pid 5912) 
Compiled ajp.utilities.js (32ms) (pid 5912) 
Served asset /application-9a36deba94d6308ab9bebe4e30a93959.js - 304 Not Modified (110802ms) 
cache: [GET /assets/logo_80px_80px-27d7b084daa398621574b10963529e84.png] miss 


Started GET "/assets/logo_80px_80px-27d7b084daa398621574b10963529e84.png" for 127.0.0.1 at 2011-12-20 00:03:13 +0000 
Served asset /logo_80px_80px-27d7b084daa398621574b10963529e84.png - 304 Not Modified (3ms) 
cache: [GET /assets/hello.png] miss 


Started GET "/assets/hello.png" for 127.0.0.1 at 2011-12-20 00:03:13 +0000 
Served asset /hello.png - 404 Not Found (3ms) 

ActionController::RoutingError (No route matches [GET] "/assets/hello.png"): 


Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout 
(1.0ms) 

。 。 。 :-D & :-o ?

編輯3 你並不需要在生產模式先運行一次它(也就是說,它實際上是在發展運行的行爲,即做一些事情來觸發生產模式(?)重新預編譯的資產)。在使用rake預編譯資源之後,它只需要以開發模式運行,請求一個頁面(其中必需),然後在生產環境中運行並再次請求主頁足以觸發看起來像第二個預編譯的內容。

這現在倖免於我的機器完全重新啓動(贏得7-64位)。所以我認爲這是一個一致的錯誤。

是StackOverflow最好的地方繼續這個討論還是最好去Sprocket git-hub page,或者什麼?

+0

它編譯爲有效的CSS?你有沒有嘗試過使用image-url? – 2011-12-19 23:06:49

+0

是的,這是有效的CSS。在開發中工作良好。只是沒有得到生產服務。給出錯誤:'ActionController :: RoutingError(沒有路由匹配[GET]「/assets/application-b2949754ecb8dda278c219b2cf589c83.css」)'儘管文件在那裏....會有點瘋狂。 – AJP 2011-12-19 23:11:06

+0

你能不能也粘貼'config/application.rb'和'config/environments/production.rb'? – 2011-12-20 04:23:40

回答

3

我有這個問題,有一天,通過編輯production.rb文件

config.assets.compress = true 
config.assets.compile = true 
config.assets.digest = true 
config.assets.initialize_on_precompile = false 

是資產設置我結束瞭解決它。我預先編譯我的資產,同時部署capistrano。

在我編輯資產設置之前,我有完全相同的問題。 .css被請求並被編譯到我的公共/資產文件夾中,但服務器無法找到這些文件。

+1

嘿本傑明。非常感謝您的幫助。不幸的是,這個建議在我的情況下似乎不起作用,當在生產模式下運行時,仍然得到'ActionController :: RoutingError(沒有路由匹配[GET]「/assets/application-3af646520ce8a8ac4b46dae627c89a0a.css」)'。它很好地服務於.js和.png文件。多麼奇怪。 – AJP 2011-12-20 09:45:18

0

爲了簡單起見,我喜歡用.css.scss exstension及用途:

li { 
    background-image: image-url("logo_80px_80px.png"); 
} 

完全可以參考上Rails Guides在第2.2.2節中找到。

13

我有完全相同的問題,並通過在我的production.rb文件中將此選項更改爲true(默認爲false)來解決此問題。

# Disable Rails's static asset server (Apache or nginx will already do this) 
    config.serve_static_assets = true 

據我瞭解,將此選項設置爲false有道理的,如果你是Apache或nginx的,這是不是我的情況下運行。

+1

如果你使用的是Heroku,你也會希望這個設置爲false。 – 2013-07-15 02:18:27

相關問題