2011-03-30 29 views
34

圖像路徑我有一個圖片,其中包含carrierwave上傳完整的URL:在Rails的3

Image.find(:first).image.url #=> "/uploads/image/4d90/display_foo.jpg" 

在我看來,我想找到這個絕對URL。在root_url結果中加上雙重/

root_url + image.url #=> http://localhost:3000//uploads/image/4d90/display_foo.jpg 

我不能使用url_for(我知道的),因爲無論是允許傳遞路徑,選項列表標識資源和:only_path選項。由於我沒有可通過「控制器」+「操作」識別的資源,因此我無法使用:only_path選項。

url_for(image.url, :only_path => true) #=> wrong amount of parameters, 2 for 1 

什麼是在Rails3中創建完整路徑的最簡潔和最好的方法?

回答

33

嘗試path方法

Image.find(:first).image.path 

UPD

request.host + Image.find(:first).image.url 

,你可以把它包裝成一個幫手永遠擦乾

request.protocol + request.host_with_port + Image.find(:first).image.url 
+0

返回整個(系統)的路徑。 '=>「/ home /..../ public/uploads/image/4d90/foo.jpg」'我需要網址,而不是路徑,用於原子提要。 – berkes 2011-03-30 10:04:04

+0

哦,是的。然後嘗試'request.host + Image.find(:first).image.url' – fl00r 2011-03-30 10:14:04

+1

也不是:('「localhost/uploads/image/4d90/display_foo.jpg」'它錯過了:3000端口(開發中)和http(s)://。 – berkes 2011-03-30 10:19:35

5

只是把地板的答案,並提供幫手:

# Use with the same arguments as image_tag. Returns the same, except including 
# a full path in the src URL. Useful for templates that will be rendered into 
# emails etc. 
def absolute_image_tag(*args) 
    raw(image_tag(*args).sub /src="(.*?)"/, "src=\"#{request.protocol}#{request.host_with_port}" + '\1"') 
end 
+2

這個幫手在標準視圖中很好地工作,但是你不能在Mailer視圖中使用它,因爲Mailer沒有「請求」的意義,所以你的幫手會失敗。 – Eric 2012-01-21 21:15:02

+0

是的。我不確定,但我認爲,通過Rails 3.1和資產管道,您可以使用資產主機來整合完整的URL。在較早的版本中,我認爲需要對主機進行硬編碼。 – 2012-01-23 09:19:25

+0

@ M.G.Palmer會繼續工作,因爲carrierwave上傳的圖片駐留在公共文件夾內? – lulalala 2012-03-16 03:25:18

9

另一種簡單的方法來使用是URI.parse,你的情況是

require 'uri' 

(URI.parse(root_url) + image.url).to_s 

和一些例子:

1.9.2p320 :001 > require 'uri' 
=> true 
1.9.2p320 :002 > a = "http://asdf.com/hello" 
=> "http://asdf.com/hello" 
1.9.2p320 :003 > b = "/world/hello" 
=> "/world/hello" 
1.9.2p320 :004 > c = "world" 
=> "world" 
1.9.2p320 :005 > d = "http://asdf.com/ccc/bbb" 
=> "http://asdf.com/ccc/bbb" 
1.9.2p320 :006 > e = "http://newurl.com" 
=> "http://newurl.com" 
1.9.2p320 :007 > (URI.parse(a)+b).to_s 
=> "http://asdf.com/world/hello" 
1.9.2p320 :008 > (URI.parse(a)+c).to_s 
=> "http://asdf.com/world" 
1.9.2p320 :009 > (URI.parse(a)+d).to_s 
=> "http://asdf.com/ccc/bbb" 
1.9.2p320 :010 > (URI.parse(a)+e).to_s 
=> "http://newurl.com" 
+0

非常優雅! – 2012-08-31 16:34:04

1

不能參照要求對象在一封電子郵件中,如何:

def image_url(*args) 
    raw(image_tag(*args).sub /src="(.*?)"/, "src=\"//#{ActionMailer::Base.default_url_options[:protocol]}#{ActionMailer::Base.default_url_options[:host]}" + '\1"') 
end 
91

您還可以設置CarrierWave的asset_host配置設置是這樣的:

# config/initializers/carrierwave.rb 
CarrierWave.configure do |config| 
    config.storage = :file 
    config.asset_host = ActionController::Base.asset_host 
end 

這^告訴CarrierWave使用你的應用程序的config.action_controller.asset_host設置,您也可以在config/envrionments/[environment].rb的一個文件來定義。 See here欲瞭解更多信息。

或明確設置:

config.asset_host = 'http://example.com' 

重新啓動應用程序,你是好去 - 不需要輔助方法。

*我用Rails 3.2和0.7.1 CarrierWave

2

有相當一堆答案在這裏。但是,我不喜歡它們中的任何一個,因爲它們都依賴於我記住明確添加端口,協議等。我覺得這是這樣做的最優雅的方式:

full_url = URI(root_url) 
full_url.path = Image.first.image.url 
# Or maybe you want a link to some asset, like I did: 
# full_url.path = image_path("whatevar.jpg") 
full_url.to_s 

,什麼是最好的事情它是我們可以很容易地改變只是一兩件事,不管是什麼東西,可能是你一直做的同樣的方式。說如果你想放棄協議,並使用The Protocol-relative URL,那麼在最後轉換爲字符串之前執行此操作。

full_url.scheme = nil 

耶,現在我有我的轉換資產圖像的URL協議,我可以使用的代碼片段別人可能希望在其網站上添加並不管他們會工作在協議的相對URL的方式他們在他們的網站上使用(假定您的網站支持任一協議)。

1

實際上,你可以很容易地得到這個由

root_url[0..-2] + image.url 

做我同意它看起來並不太好,但能夠完成任務.. :)

2

我用default_url_options,因爲request不在郵件程序中可用,並避免在config.action_controller.asset_host中複製主機名,如果以前沒有指定它的話。

config.asset_host = ActionDispatch::Http::URL.url_for(ActionMailer::Base.default_url_options) 
+0

非常感謝! – 2018-01-03 09:32:26

0

我發現這個辦法來避免雙斜線:

URI.join(root_url, image.url)