简体   繁体   English

如何从Rails控制器获取S3资产的路径

[英]How to get path to S3 assets from Rails controller

My setup 我的设定

I'm using Rails 3, asset_sync and paperclip. 我正在使用Rails 3,asset_sync和回形针。

  • When I'm developing on my local machine, I want assets to be served through asset pipeline . 在本地计算机上进行开发时,我希望通过资产管道来提供资产
  • In production, I want assets to be served from S3 . 在生产中,我希望从S3提供资产。

My question 我的问题

How can I access to the asset path from Controller/Model/whatever? 如何从控制器/模型/任何地方访问资产路径? (The same URL that I get if I use image_path("favicon.ico") in views) (如果在视图中使用image_path("favicon.ico") ,则获得的URL相同)

I have seen countless of SO questions, where the accepted answer suggest use of ActionController::Base.helpers.asset_path("favicon.ico") . 我看到了无数的SO问题,其中可接受的答案建议使用ActionController::Base.helpers.asset_path("favicon.ico") That's fine, BUT, when I try it in production, I get an URL pointing to my production server, NOT to S3. 很好,但是,当我在生产环境中尝试时,我得到一个指向生产服务器而不是S3的URL。

I added a piece of test code to my production HAML to demonstrate the issue: 我在生产HAML中添加了一段测试代码来演示该问题:

%link{:rel => "image_src", :href => image_path("test.png")} %link{:rel => "image_src", :href => ActionController::Base.helpers.image_path("test.png")}

...which generates HTML: ...生成HTML:

<link href='http://mybucket.s3.amazonaws.com/assets/test-2cfd63058597b0c73221f3c1988c121e.png' rel='image_src'> <link href='/assets/test-2cfd63058597b0c73221f3c1988c121e.png' rel='image_src'>

So, I think ActionController::Base.helpers.image_path is not the right way to solve the issue. 因此,我认为ActionController :: Base.helpers.image_path不是解决问题的正确方法。

I've also seen many answers saying that view_context should be used. 我也看到了很多答案,说应该使用view_context However, I do not have instance of a controller at the time when I need the URL, so also this approach does not work for me. 但是,当我需要URL时,我没有控制器的实例,因此这种方法对我也不起作用。

More background (in case you're interested) 更多背景(以防您感兴趣)

What I'm trying to achieve is to serve Paperclip default image from S3. 我想要实现的是从S3提供Paperclip默认图像。

has_attached_file :logo, :styles => { ... }, :default_url => # Here, I need the URL that points to S3, if this is run in production.

I don't want to hard-code the URL to point to my S3 bucket, since I'd like to serve the file from local machine when I'm developing (as suggested here: https://github.com/thoughtbot/paperclip/issues/1092 ) 我不想对URL进行硬编码以指向我的S3存储桶,因为我想在开发时从本地计算机提供文件(如此处建议: https : //github.com/thoughtbot/回形针/问题/ 1092

Seems like the issue described here: https://github.com/rails/rails/issues/10051 and which seems to be fixed in Rails 4 with https://github.com/rails/rails/pull/12622 . 好像是这里描述的问题: https : //github.com/rails/rails/issues/10051 ,似乎已在Rails 4中通过https://github.com/rails/rails/pull/12622修复。

The first discussion describes a possible workaround with an initializer for Rails 3: 第一个讨论描述了Rails 3的初始化程序的可能解决方法:

class ActionController::Base
  def self.helpers
    @helper_proxy ||= begin
      proxy = ActionView::Base.new
      proxy.config = config.inheritable_copy
      proxy.extend(_helpers)
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM