问题描述
我有一张图片,其中包含载波上传:
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?
推荐答案
尝试path
方法
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!