2012-07-29 157 views
6

我卡住了! xD我一直在研究Rails項目,並且無法使用相對路徑訪問我的資產。我正在處理控制器和模型時,我的一位朋友正在處理html/css一側的事情。我的朋友最近給我一批通過以下方式構建的文件:訪問rails資產的相對路徑

app/assets/images/*.jpg 
app/assets/stylesheets/*.css 
app/assets/javascripts/*.js 
app/assets/fonts/*.* (+some more css files in here) 

在我的應用程序/視圖/佈局目錄中,我有一個名爲佈局其final.html.erb用於我的整個Web應用程序。我也有1頁(HTML正文內容),我試圖用app/views /頁面中的此佈局進行渲染,名爲final_page.html.erb ...必要的路由適合頁面加載;但是,它只加載final_page.html.erb的上下文(無圖像,樣式或字體)。當我去控制檯並輸入「rails server」並訪問localhost:3000時,頁面顯示出來......裸體大聲笑。控制檯輸出如下:


Started GET "/" for 127.0.0.1 at 2012-07-28 21:15:02 -0700 
Connecting to database specified by database.yml 
Processing by PagesController#final_page as HTML 
    Rendered pages/final_page.html.erb within layouts/final (8.4ms) 
Completed 200 OK in 82ms (Views: 81.0ms | ActiveRecord: 0.0ms) 


Started GET "/assets/stylesheets/style.css" for 127.0.0.1 at 2012-07-28 21:15:04 -0700 

ActionController::RoutingError (No route matches [GET] "/assets/stylesheets/style.css"): 
    actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' 
    actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call' 
    railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app' 
    railties (3.2.6) lib/rails/rack/logger.rb:16:in `call' 
    actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call' 
    rack (1.4.1) lib/rack/methodoverride.rb:21:in `call' 
    rack (1.4.1) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.4.1) lib/rack/lock.rb:15:in `call' 
    actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call' 
    railties (3.2.6) lib/rails/engine.rb:479:in `call' 
    railties (3.2.6) lib/rails/application.rb:220:in `call' 
    rack (1.4.1) lib/rack/content_length.rb:14:in `call' 
    railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call' 
    rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service' 
    /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service' 
    /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run' 
    /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread' 


    Rendered /var/lib/gems/1.9.1/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.0ms) 

我覺得現在的問題是,我試圖訪問使用相對路徑的文件。我的佈局文件看起來是這樣的:

<!doctype html> 
<html class="no-js"> 
<head> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width,initial-scale=1"> 
<meta name="viewport" content="width=device-width"> 
<title>:: Final ::</title> 
<link rel="stylesheet" type="text/css" href="../../assets/stylesheets/style.css"> 
<link href="../../assets/stylesheets/desktop.css" rel="stylesheet" type="text/css" media="only screen and (min-width:769px) and (max-width:1000px)"> 
<script src="../../assets/javascripts/modernizr.js" type="text/javascript"></script> 
</head> 
<body> 
    <%= yield %> 
</body> 
</html> 

而且,我final_page.html.erb文件的正文中,我嘗試訪問使用相對路徑以及圖片...像這樣:

<img src="../../assets/images/mainImg.jpg" alt="img"> 

我的朋友寫了大部分這個html,並且他有0個使用rails的經驗。我決定改變,像這樣對資產的要求:

來源:

<link rel="stylesheet" type="text/css" href="../../assets/stylesheets/style.css"> 

要:

<%= stylesheet_link_tag "application.css" %> 

來源:

<script src="../../assets/javascripts/modernizr.js" type="text/javascript"></script> 

要:

<%= javascript_include_tag "application.js" %> 

來源:

<img src="../../assets/images/mainImg.jpg" alt="img"> 

要:

<%= image_tag "mainImg.jpg" %> 

這多少有些幫助,如圖像加載,很少的造型來通過;然而,它遠離它應該看起來的樣子。我想這是因爲我的朋友讓親戚通話中的CSS文件本身:

body { 
    background-image: url(../images/bg.jpg); 
    background-repeat: repeat; 
} 

我試着網址替換這些(<%= asset_path「bg.jpg」%>)等等 但它沒有效果。我嘗試了很多東西,並閱讀了很多帖子。我想知道Rails是否允許我使用相對路徑。我試過了:

config.assets.enabled = false 

但這並沒有幫助...請......我做錯了什麼?我不認爲我的朋友會想要停止使用他的工作的相對路徑,因爲這是他如何做事。該網站在Rails之外很好地啓動,但我需要它與我的Web應用程序的Rails一起工作。任何建議將不勝感激。感謝您耐心閱讀所有這些內容。

P.S.我正在使用Ruby 1.9.3和Rails 3.2.6

回答

6

嘗試使用/assets/style.css而不是/assets/stylesheets/style.css等等。

如果有這樣的結構:

app 
    assets 
    stylesheets 
    javascripts 

然後以訪問文件app/assetsapp/assets/any_folder你應該使用路徑/assets/file

更新

這裏嘗試:

body { 
    background-image: url(bg.jpg); 
    background-repeat: repeat; 
} 

或者:

body { 
    background-image: url(/assets/bg.jpg); 
    background-repeat: repeat; 
} 
+0

這實際上解決了50%問題xD非常感謝!但是,造型仍然關閉。我不確定是因爲css文件使用相對路徑還是? :(我將不得不進入CSS文件,並改變它指向圖像的路徑?像: body { background-image:url(../ images/bg.jpg); background-repeat:repeat ; } 從url(../ images/bg.jpg)到url(/assets/bg.jpg)? – Orlin 2012-07-29 06:31:56

+0

哦,我的話......那100%的工作......我使用了url(/ assets/bg.jpg)修復了所有在css文件中的圖像引用,它做了訣竅!!!謝謝你做到這麼多! – Orlin 2012-07-29 06:44:28

+1

是的。謝謝:) – Orlin 2012-07-29 06:57:38

4

的Rails 3.1中引入新的資產管道。我建議你讀一遍這樣的:http://guides.rubyonrails.org/asset_pipeline.html

在您的應用程序/視圖/佈局/ application.html.erb文件,請確保你有這樣的:

<%= stylesheet_link_tag "application", :media => "all" %> 

在您的應用程序/資產/樣式表/ application.css文件,請確保您有這行:

*= require_tree . 

(這將確保您在應用程序/資產/ STYL有任何CSS文件esheets目錄將提供給您的應用程序)

在你的CSS文件,你可以這樣做是爲了在你的應用程序/資產/ images目錄參考圖像:

background-image: url(bg.jpg); 
+1

字體在另一個目錄呢? – 2014-06-09 16:33:54

+0

對於字體,我認爲你需要添加這行到config/application.rb: config.assets.paths << Rails.root.join(「app」,「assets」,「fonts」) – calasyr 2015-05-22 20:47:43