Ruby-on-rails 获取回形针附件的绝对 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12550530/
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-02 20:47:42  来源:igfitidea点击:

Get absolute URL for paperclip attachment

ruby-on-railspaperclip

提问by sleepy_keita

Is it possible to get the absolute URI for a Paperclip attachment? Right now, the problem is that the production environment is deployed in a sub-URI (on Passenger: RackBaseURI), but <paperclip attachment>.urlreturns the Rails-app relative URI (/system/images/...). Is there a way to get the absolute URI for Paperclip attachments?

是否可以获得回形针附件的绝对 URI?现在,问题是生产环境部署在子 URI 中(在Passenger: 上RackBaseURI),但<paperclip attachment>.url返回Rails-app 相对URI ( /system/images/...)。有没有办法获得 Paperclip 附件的绝对 URI?

I'm using Paperclip v2.7 and Rails 3.2.8.

我正在使用 Paperclip v2.7 和 Rails 3.2.8。

回答by Chris Bosco

asset_url(model.attachment_name.url(:style))

Relevant github issue

相关github问题

回答by kuboon

try

尝试

URI.join(request.url, @model.attachment_name.url)

or

或者

URI(request.url) + @model.attachment_name.url

It's safe if you use S3 or absolute url.

如果您使用 S3 或绝对 url,则是安全的。

Update: this answer is better than mine ;) https://stackoverflow.com/a/21027839/683157

更新:这个答案比我的好;) https://stackoverflow.com/a/21027839/683157

回答by Augustin Riedinger

According to this github issue, it is cleaner to use ActionController::Base.asset_hostso it would result the helper:

根据这个github issue,使用起来更干净,ActionController::Base.asset_host所以它会导致助手:

  def add_host_prefix(url)
    URI.join(ActionController::Base.asset_host, url)
  end

This supposes you have in every /config/environments/<environment>.rbfile the following:

这假设您在每个/config/environments/<environment>.rb文件中都有以下内容:

Appname::Application.configure do

  # ....

  config.action_controller.asset_host = 'http://localhost:3000' # Locally

  # ....

end

回答by Paul Odeon

The most widely applicable way of doing this is to first define your asset hosts in the relevant config/environment file:

最广泛适用的方法是首先在相关的配置/环境文件中定义您的资产主机:

config.action_controller.asset_host = "http://assethost.com"
config.action_mailer.asset_host = "http://assethost.com"

Then in views and mailers:

然后在视图和邮件中:

asset_url(model.attachment.url(:style))

In the console:

在控制台中:

helper.asset_url(model.attachment.url(:style))

In a model:

在模型中:

ApplicationController.helpers.asset_url(model.attachment.url(:style))

回答by iouri

You can do this:

你可以这样做:

<%= image_tag "#{request.protocol}#{request.host_with_port}#{@model.attachment_name.url(:attachment_style)}" %>

Or make a helper method to wrap it.

或者做一个辅助方法来包装它。

def absolute_attachment_url(attachment_name, attachment_style = :original)
  "#{request.protocol}#{request.host_with_port}#{attachment_name.url(attachment_style)}"
end

And use it like this:

并像这样使用它:

<%= image_tag absolute_attachment_url(attachment_name, :attachment_style)}" %>

Ex: Model = Person (@person), attachment_name = avatar, style = :thumb

例如:模型 = 人 (@person),附件名称 = 头像,样式 = :thumb

<%= image_tag absolute_attachment_url(@person.avatar, :thumb)}" %>

回答by blahedo

This doesn't solve the original poster's problem exactly (it operates in the view, not the model), but may be helpful for people who are trying to "get absolute URL for paperclip attachment" within their view: In the same way that

这并不能完全解决原始海报的问题(它在视图中运行,而不是在模型中运行),但可能对试图在其视图中“获取回形针附件的绝对 URL”的人有所帮助:以同样的方式

image_tag(user.avatar.url(:large))

puts the image itself into your view,

将图像本身放入您的视图中,

image_url(user.avatar.url(:large))

returns just the URL you'll need if you want to link to the asset directly (e.g. in a link_tocall).

如果您想直接链接到资产(例如在link_to通话中),则仅返回您需要的 URL 。

回答by micred

You can add to your application.rb(or for specific enviroment in config/environments/*):

您可以添加到您的application.rb(或针对 中的特定环境config/environments/*):

config.paperclip_defaults = {
    url: "http://my.address.com/system/:class/:attachment/:id_partition/:style.:extension",
    path: ':rails_root/public/system/:class/:attachment/:id_partition/:style.:extension',
}

Restart and reimport your images.

重新启动并重新导入您的图像。

PS: obviously you can replace http://my.address.comwith an environment variable.

PS:显然你可以用环境变量替换http://my.address.com