[英]Rails 3: Create new rails asset helper like image_tag that points to s3
What would be the best way to create a new asset tag helper that would work the exact same way as image_tag, except pointed to s3? 除了指向s3之外,创建一个与image_tag完全相同的新资产标签帮助器的最佳方法是什么?
So I'd like this to happen: 所以我希望这件事发生:
image_path "foo.jpg" # => "/assets/foo.jpg" (default behavior)
s3_image_path "bar.jpg" # => "http://site.s3.amazon.com/assets/bar.jpg"
Would the best way to do this be just to make a new helper in application_helper.rb? 最好的方法是在application_helper.rb中创建一个新助手吗?
I just ended up writing new helpers in application_helper.rb that mimicked the default asset helpers like so: 我刚刚在application_helper.rb中编写新的帮助程序,模仿默认的资产助手,如下所示:
def s3_image_path(filename)
"http://site.s3.amazonaws.com/assets/#{filename}"
end
def s3_image_tag(filename, options={})
image_tag(s3_image_path(filename), options)
end
So s3_image_tag("image.jpg")
will point to http://site.s3.amazonaws.com/assets/image.jpg
. 所以s3_image_tag("image.jpg")
将指向http://site.s3.amazonaws.com/assets/image.jpg
。
If site.s3.. is not in the rails application and not using the asset_pipeline, then just reference the url directly in the image src. 如果site.s3 ..不在rails应用程序中而不使用asset_pipeline,那么只需在图像src中直接引用url。
If s3 is your application site, then yes just a write a new helper in the context it will be used for. 如果s3是你的应用程序站点,那么只需在它将用于的上下文中编写一个新助手。
Or something for http://site.s3.amazon.com/assets/bar.jpg would look like 或者http://site.s3.amazon.com/assets/bar.jpg看起来像
<img src="<%=@root_url%>/assets/bar.jpg" />
So, why is the other one different? 那么,为什么另一个不同呢? I mean why do you have two? 我的意思是你为什么有两个?
Edit: 编辑:
What I would do is just have a string holding the url to the hosted site in the application helper. 我要做的就是在应用程序助手中有一个字符串,将url保存到托管站点。 Then the helper method to return the image tag("img", :src => "#{s3_image_link}/bar.jpg")
然后是帮助方法返回图像tag("img", :src => "#{s3_image_link}/bar.jpg")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.