0

我正在使用 attachment_fu 和 Amazon S3 存储开发 Rails 应用程序。是否可以在创建对象时根据用户输入使 :s3_ 访问(has_attachment 选项的一部分)有条件。我希望用户能够选择附件是经过身份验证读取还是公开读取。这可能吗?您将如何设置条件语句?

我的模型如下所示:

has_attachment :content_type => :image,
               :storage => :s3,
               :size => 0.kilobytes..6144.kilobytes,
               :processor => 'Rmagick',
               :resize_to => '650x500>',
               :thumbnails => { :thumb => '75x75!' },
               :s3_access => ( [[conditional]] ? 'authenticated-read' : 'public-read' )

显然 [[conditional]] 是我要替换的内容,但我不知道如何根据模型中的用户操作正确设置项目的条件。也许这是这种类型语句的条件错误时间。有什么建议么?

4

1 回答 1

0

James, I've done this before. I was digging through my old code, trying to find a sample I could show here, but I can't seem to find it. Old client, code is probably gone, etc...

You are on the right track though. Remember, 'has_attachment' is just a method call with a hash for the parameters, so if you treat it as such, you can adjust which hash values are ultimately sent to the method.

Edit - Here is a code sample -

production = ENV['RAILS_ENV'] == 'production'

has_attachment :content_type => :image, 
  :max_size => 1.megabyte, 
  :resize_to => '800x600>', 
  :thumbnails => { :thumb => '146x146>', :small => '75x75' }, 

  # skip s3 for local development and testing
  :storage => (production ? :s3 : :file_system),
  :path_prefix => (production ? 'comment_images' : 'public/comment_images')

In the above example, I wanted to store uploads locally while developing, but switch to S3 when in production. So you are very close to where you need to be...

于 2009-06-20T21:16:33.520 回答