sle*_*ita 24 ruby-on-rails paperclip
是否可以获取Paperclip附件的绝对URI?现在,问题是生产环境部署在子URI(在Passenger :)上RackBaseURI
,但<paperclip attachment>.url
返回Rails-app相对URI(/system/images/...
).有没有办法获得Paperclip附件的绝对URI?
我正在使用Paperclip v2.7和Rails 3.2.8.
Chr*_*sco 41
asset_url(model.attachment_name.url(:style))
Run Code Online (Sandbox Code Playgroud)
kub*_*oon 28
尝试
URI.join(request.url, @model.attachment_name.url)
Run Code Online (Sandbox Code Playgroud)
要么
URI(request.url) + @model.attachment_name.url
Run Code Online (Sandbox Code Playgroud)
如果您使用S3或绝对网址,这是安全的.
更新:这个答案比我的好;)/sf/answers/1471948761/
Aug*_*ger 18
根据这个github问题,它使用ActionController::Base.asset_host
起来更干净,因此它会产生帮助:
def add_host_prefix(url)
URI.join(ActionController::Base.asset_host, url)
end
Run Code Online (Sandbox Code Playgroud)
假设您在每个/config/environments/<environment>.rb
文件中都有以下内容:
Appname::Application.configure do
# ....
config.action_controller.asset_host = 'http://localhost:3000' # Locally
# ....
end
Run Code Online (Sandbox Code Playgroud)
最广泛适用的方法是首先在相关的配置/环境文件中定义资产主机:
config.action_controller.asset_host = "http://assethost.com"
config.action_mailer.asset_host = "http://assethost.com"
Run Code Online (Sandbox Code Playgroud)
然后在视图和邮件中:
asset_url(model.attachment.url(:style))
Run Code Online (Sandbox Code Playgroud)
在控制台中:
helper.asset_url(model.attachment.url(:style))
Run Code Online (Sandbox Code Playgroud)
在模型中:
ApplicationController.helpers.asset_url(model.attachment.url(:style))
Run Code Online (Sandbox Code Playgroud)
小智 5
你可以这样做:
<%= image_tag "#{request.protocol}#{request.host_with_port}#{@model.attachment_name.url(:attachment_style)}" %>
Run Code Online (Sandbox Code Playgroud)
或者使用辅助方法来包装它.
def absolute_attachment_url(attachment_name, attachment_style = :original)
"#{request.protocol}#{request.host_with_port}#{attachment_name.url(attachment_style)}"
end
Run Code Online (Sandbox Code Playgroud)
并像这样使用它:
<%= image_tag absolute_attachment_url(attachment_name, :attachment_style)}" %>
Run Code Online (Sandbox Code Playgroud)
例如:Model = Person(@person),attachment_name = avatar,style =:thumb
<%= image_tag absolute_attachment_url(@person.avatar, :thumb)}" %>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27674 次 |
最近记录: |