Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 为S3动态生成bucket名称_Ruby On Rails 3_Amazon S3_Paperclip - Fatal编程技术网

Ruby on rails 3 为S3动态生成bucket名称

Ruby on rails 3 为S3动态生成bucket名称,ruby-on-rails-3,amazon-s3,paperclip,Ruby On Rails 3,Amazon S3,Paperclip,我试图根据多态关联类型创建一个动态bucket名称 我的第一个方法是尝试这样的方法: class PostImage < ActiveRecord::Base belongs_to :imageable, :polymorphic => true has_attached_file :image, :styles => { :small => "200x200>", :thumb => "50x50>" },

我试图根据多态关联类型创建一个动态bucket名称

我的第一个方法是尝试这样的方法:

class PostImage < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true

  has_attached_file :image, :styles => { :small => "200x200>", :thumb => "50x50>" },
                            :storage => :s3, 
                            :s3_credentials => "#{Rails.root}/config/s3.yml", 
                            :path => "/:style/:id/:filename", 
                            :bucket => self.imageable_type.to_s

end
class PostImagetrue
已附加文件:image,:styles=>{:small=>“200x200>”,:thumb=>“50x50>”,
:storage=>:s3,
:s3_credentials=>“#{Rails.root}/config/s3.yml”,
:path=>“/:style/:id/:filename”,
:bucket=>self.imageable\u type.to\u s
结束
如果尝试创建新对象,则会出现下一个错误:

NoMethodError:0x007fd3fe0b15d8的未定义方法“imageable_type”

我在S3文档中发现:

bucket:这是存储文件的S3 bucket的名称。记住,bucket在所有AmazonS3中必须是唯一的。如果桶不存在,回形针将尝试创建它。不会插入存储桶名称。如果要在运行时确定bucket的名称,可以将其定义为Proc。回形针将调用附件作为唯一参数的进程

问题是我不知道如何将多态关联的名称设置为bucket的名称

任何帮助都将不胜感激。

希望它能帮助别人

最终的解决方案基于以下内容:

阅读文章,更好地解释如何使用程序

最终代码:

class PostImage < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true

  has_attached_file :image, :styles => {
                   :square=> "170x170#",
                   :rectangle=> "220x170#",
                   :large  => "600x400#" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml",
                   :path => "/:style/:id/:filename",
                   :bucket =>  lambda { |attachment| "#{attachment.instance.imageable_type.to_s.downcase}-gallery" }


end
class PostImagetrue
已附加文件:图像,:样式=>{
:square=>“170x170#”,
:矩形=>“220x170#”,
:large=>“600x400”},:storage=>:s3,:s3_credentials=>“#{Rails.root}/config/s3.yml”,
:path=>“/:style/:id/:filename”,
:bucket=>lambda{| attachment |“#{attachment.instance.imageable_type.to_s.downcase}-gallery”}
结束