【问题标题】:Paperclip::AdapterRegistry::NoHandlerError nested data and storage s3Paperclip::AdapterRegistry::NoHandlerError 嵌套数据和存储 s3
【发布时间】:2013-05-08 09:59:20
【问题描述】:

我正在使用 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,但没有帮助。 我尝试关注this 的回答,但我不确定在哪里将参数传递给回形针?

谁知道问题出在哪里?谢谢

【问题讨论】:

  • 只是我在您的回形针配置中注意到的...您的存储桶在哪里?您实际上在哪里保存图像?您需要在您的 aws 帐户中创建一个存储桶
  • 你的意思是::storage => :s3, :s3_credentials => "#{Rails.root}/config/amazon_s3.yml" 我不知道你说的桶是什么意思。我很抱歉。但我在其他模型(不是nested_form)中使用了回形针和s3,它可以工作
  • 是什么意思?认为你错过了什么
  • 是的,抱歉,刚刚编辑过

标签: ruby-on-rails-3 amazon-s3 paperclip nested-forms


【解决方案1】:

好的,从我所见,您似乎缺少一个(存储桶)位置来在 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 账户中创建存储桶

【讨论】:

  • 即使在我的 lib>paperclip>storage 中我已经有 s3.rb,我仍然需要在回形针配置中添加 :bucket 吗?因为在其他课程中我也使用回形针,没有添加桶
  • 好的,那么您如何通过 AWS 查看这些图像,因为您可以登录您的帐户并查看您的所有资产吗?我就是这样做的,过去没有任何问题,试试吧?
  • 这不是问题,不幸的是:(我想我现在知道问题所在了。我已经把这个 { :multipart => true })在我的 _form.html.erb 中执行 |f| %> 当我看到“查看页面源代码”时,我看到了 multipart => true 但是当我“检查元素”时,我没有看到 multipart。
  • 如果您使用 form_for,您不需要设置 multipart => true,这已经为您完成 guides.rubyonrails.org/form_helpers.html#uploading-files 如果您使用 form_tag,那么是的,您需要使用它
猜你喜欢
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 2018-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多