Ruby-on-rails Rails 3 中图像路径的完整 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5484809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:48:08  来源:igfitidea点击:

Full url for an image-path in Rails 3

ruby-on-railsrubyruby-on-rails-3urlpath

提问by berkes

I have an Image, which contains carrierwave uploads:

我有一个图像,其中包含载波上传:

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

In my view, I want to find the absolute url for this. Appending the root_urlresults in a double /.

在我看来,我想为此找到绝对网址。附加root_url 会导致 double /

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

I cannot use url_for(that I know of), because that eitherallows passing a path, ora list of options to identify the resource and the :only_pathoption. Since I do't have a resource that can be identified trough "controller"+"action" I cannot use the :only_pathoption.

我不能使用url_for(我知道的),因为无论是允许通过的路径,或者选项标识资源和列表:only_path选项。由于我没有可以通过“控制器”+“操作”识别的资源,因此我无法使用该:only_path选项。

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

What would be the cleanest and best way to create a path into a full url in Rails3?

在 Rails3 中创建路径到完整 url 的最干净和最好的方法是什么?

采纳答案by fl00r

try pathmethod

尝试path方法

Image.find(:first).image.path

UPD

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

回答by ncherro

You can also set CarrierWave's asset_hostconfig?setting like this:

您还可以asset_host像这样设置 CarrierWave 的config?setting:

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

This ^ tells CarrierWave to use your app's config.action_controller.asset_hostsetting, which can be defined in one of your config/envrionments/[environment].rbfiles. See herefor more info.

这个 ^ 告诉 CarrierWave 使用您的应用程序的config.action_controller.asset_host设置,该设置可以在您的config/envrionments/[environment].rb文件之一中定义。 请参阅此处了解更多信息。

Or set it explicitly:

或者明确设置:

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

Restart your app, and you're good to go - no helper methods required.

重新启动您的应用程序,您就可以开始使用了 - 不需要辅助方法。

* I'm using Rails 3.2 and CarrierWave 0.7.1

* 我使用的是 Rails 3.2 和 CarrierWave 0.7.1

回答by c2h2

Another simple method to use is URI.parse, in your case would be

另一个简单的使用方法是 URI.parse,在你的情况下是

require 'uri'

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

and some examples:

和一些例子:

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" 

回答by Martin T.

Just taking floor's answer and providing the helper:

只是听取发言的答案并提供帮助者:

# 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}" + '"')
end

回答by Timo

There's quite a bunch of answers here. However, I didn't like any of them since all of them rely on me to remember to explicitly add the port, protocol etc. I find this to be the most elegant way of doing this:

这里有很多答案。但是,我不喜欢它们中的任何一个,因为它们都依赖于我来记住明确添加端口、协议等。我发现这是执行此操作的最优雅的方式:

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

And what is the best thing about it is that we can easily change just one thing and no matter what thing that might be you always do it the same way. Say if you wanted to drop the protocol and and use the The Protocol-relative URL, do this before the final conversion to string.

它最好的一点是,我们可以轻松地改变一件事,无论是什么事情,你总是以同样的方式去做。假设您想删除协议并使用The Protocol-relative URL,请在最终转换为字符串之前执行此操作。

full_url.scheme = nil

Yay, now I have a way of converting my asset image urls to protocol relative urls that I can use on a code snippet that others might want to add on their site and they'll work regardless of the protocol they use on their site (providing that your site supports either protocol).

是的,现在我有一种方法可以将我的资产图像 url 转换为协议相关 url,我可以在其他人可能想要添加到他们网站上的代码片段上使用它,无论他们在他们的网站上使用什么协议,他们都可以工作(提供您的站点支持任一协议)。

回答by Eugene

I used default_url_options, because requestis not available in mailer and avoided duplicating hostname in config.action_controller.asset_hostif haven't specified it before.

我使用了default_url_options, 因为request在邮件程序中不可用,并且config.action_controller.asset_host如果之前没有指定它,则避免在其中复制主机名。

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

回答by hlcs

I found this trick to avoid double slash:

我发现了这个避免双斜线的技巧:

URI.join(root_url, image.url)

回答by Alfie

You can actually easily get this done by

您实际上可以通过以下方式轻松完成此操作

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

I agree it doesn't look too good, but gets the job done.. :)

我同意它看起来不太好,但完成了工作.. :)

回答by Luke W

You can't refer to request object in an email, so how about:

您不能在电子邮件中引用请求对象,那么如何:

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