Rails 3 中图像路径的完整 url [英] Full url for an image-path in Rails 3
问题描述
我有一个图像,其中包含载波上传:
I have an Image, which contains carrierwave uploads:
Image.find(:first).image.url #=> "/uploads/image/4d90/display_foo.jpg"
在我看来,我想为此找到绝对网址.附加 root_url 导致双 /
.
In my view, I want to find the absolute url for this. Appending the root_url results in a double /
.
root_url + image.url #=> http://localhost:3000//uploads/image/4d90/display_foo.jpg
我不能使用 url_for(我知道),因为要么允许传递一个路径,或一个选项列表来识别资源和:only_path
选项.由于我没有可以通过控制器"+操作"识别的资源,因此我无法使用 :only_path
选项.
I cannot use url_for (that I know of), because that either allows passing a path, or a list of options to identify the resource and the :only_path
option. Since I do't have a resource that can be identified trough "controller"+"action" I cannot use the :only_path
option.
url_for(image.url, :only_path => true) #=> wrong amount of parameters, 2 for 1
在 Rails3 中创建路径到完整 url 的最干净和最好的方法是什么?
What would be the cleanest and best way to create a path into a full url in Rails3?
推荐答案
try path
method
try path
method
Image.find(:first).image.path
UPD
request.host + Image.find(:first).image.url
你可以把它包裹起来作为永远干燥它的帮手
and you can wrap it as a helper to DRY it forever
request.protocol + request.host_with_port + Image.find(:first).image.url
这篇关于Rails 3 中图像路径的完整 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!