1

我正在使用 Paperclip 上传多个图像并将其存储在 s3 中。所以,我有一个画廊模型,看起来像这样:

class Gallery < ActiveRecord::Base
    attr_accessible :title, :body, :pictures_attributes
    has_many :pictures
    accepts_nested_attributes_for :pictures, :allow_destroy => true


end

和画廊应该有很多图片。我的图片模型如下所示:

class Picture < ActiveRecord::Base
 belongs_to :gallery
   has_attached_file :picture, :styles => { :small => "150x150>", :medium => "300x300"  },
                      :storage => :s3,
                      :s3_credentials => "#{Rails.root}/config/amazon_s3.yml",
                      :path => "/:class/:style/:id/:filename"                 

      validates_attachment_presence :picture
      validates_attachment_size :picture, :less_than => 5.megabytes
      validates_attachment_content_type :picture, :content_type => ['image/jpeg', 'image/png']

end

我已经把它放在了我的 _form.html.erb 中:

<%= form_for @gallery, :html => { :multipart => true } do |f| %>

这也是

<%= f.fields_for :picture do |picture_form| %>
        <p>
          <%= picture_form.file_field :picture %>
        </p>
 <% end %>

在我的画廊控制器中,我有这个:

def new
        @gallery = Gallery.new
        5.times{ @gallery.pictures.build }
    end

      # GET /galleries/1/edit
    def edit
        @gallery = Gallery.find(params[:id])
        5.times{ @gallery.pictures.build }
    end

      # POST /galleries
      # POST /galleries.xml
    def create
      @gallery = Gallery.new(params[:gallery])
      respond_to do |format|
          if @gallery.save
            format.html { redirect_to(admin_gallery_path(@gallery), :notice => 'Gallery was successfully created.') }
            format.xml  { render :xml => @gallery, :status => :created, :location => @gallery }
          else
            format.html { render :action => "new" }
            format.xml  { render :xml => @gallery.errors, :status => :unprocessable_entity }
          end

        end
end

我发现了一些类似的案例,并遵循了答案。但我仍然收到相同的错误消息。我试图将 RAILS_ROOT 更改为 Rails.root,但没有帮助。我试图遵循这个答案,但我不确定在哪里将参数传递给回形针?

任何人都知道问题是什么?谢谢

4

1 回答 1

0

好的,从我所见,您似乎缺少在 AWS 中存储图片的(存储桶)位置。我会向你展示我的意思的一个例子

 has_attached_file :avatar, 
 :styles => {:thumb => "100x100>" },
 :storage => :s3,
 :s3_credentials => "#{Rails.root}/config/s3.yml",
 :path => "/images/:id/:style.:extension",
 :url  => ":s3_domain_url",
 :bucket => "assets.recipesapp"

您在 AWS 账户中创建存储桶

于 2013-05-08T10:18:36.653 回答