【问题标题】:Rails Paperclip Error: No handler found forRails Paperclip 错误:找不到处理程序
【发布时间】:2017-09-13 11:57:22
【问题描述】:

这是一个很常见的错误,我已经回答了很多问题,但对我没有任何帮助。你能帮忙吗,我哪里出错了?

场景:我有一个商品和图片表,当用户添加商品时,他/她必须添加至少一张图片。当我在附加图像后保存表单时,它会出错。下面附上sn-ps代码和错误截图:

item.rb

class Item < ApplicationRecord
  belongs_to :user, class_name: 'User', foreign_key: :user_id
  has_many :images, class_name: 'Image', foreign_key: :item_id, dependent: :destroy
  accepts_nested_attributes_for :images, allow_destroy: true
end

image.rb

class Image < ApplicationRecord
  belongs_to :item, foreign_key: :item_id, optional: true

  has_attached_file :image, styles: { small: '64x64', med: '100x100', large: '200x200' },
                    default_url: '/images/:id/:filename'
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

items_controller.rb

class ItemsController < ApplicationController

  def new
    @item = Item.new
    @item.images.build
    #new.html.erb
  end

  def create
    @item = Item.new(item_params.merge(user_id: current_user.id))
    if @item.save
      redirect_to items_path
    else
      render :new
    end
  end

  private

  def item_params
    params.require(:item).permit(:name, :category, :qty, :cost, :description,
                                 :city, :postal_code, :country,
                                 images_attributes: [:id, :item_id, :_destroy, image: []])
  end
end

new.html.erb

<div class="container">
  <h2> <% if current_user.items.blank? %> Become a seller! <% end %> Add a new item </h2> <br>
  <%= form_for @item, url: {action: :create}, html: { multipart: true } do |f| %>
      <%= item_error_messages! %>
      <div class="form-group">
        <%= f.label :name, class:'control-label col-sm-2' %>
        <div class="col-sm-10">
          <%= f.text_field :name, class: 'form-control', placeholder: 'Enter name' %>
        </div>
      </div>

      <%= f.fields_for :images, @item.images.build do |img| %>
          <%= img.file_field :image, multiple: true %>
      <% end%>

      <br><br>
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10" style="margin-bottom: 40px;">
          <%= f.submit 'Save', class:'btn btn-default' %>
        </div>
      </div>
  <% end %>
</div>

错误:

可能的问题是什么?我读过的大多数解决方案都建议在表单标签中添加multipart: true,但它已经存在了。

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby paperclip


    【解决方案1】:

    我认为您的问题与您尝试为每个模型保存多个附件的事实有关。显然Paperclip对这种情况的处理不是很好,所以你需要自己动手。

    如果您想同时保存多个文件,您可以按照该示例下方链接中的接受答案如何实现:

    只是来自以下链接的简历:

    1 - 在上面的例子中你要保存多个文件,不需要添加 multiple: true 选项,保存时会报错。

    2 - 关于参数:这将导致错误,因为模型不知道如何处理图像数组

    3 - 要同时保存多个文件,您需要使用自己的 处理程序

    Rails 4 multiple file attachments with Paperclip

    祝你好运!

    #UPDATE

    关于如何一次保存多个文件的想法:

    1 - 您需要有一个动态输入文件的表单。 所以每个输入都可以得到每个文件的条目。你可以做到这一点 编写自己的 .js 库或使用像“Cocoon”这样的 gem。你会 在这里使用嵌套属性。

    2 - 你的控制器中的强参数将接受你的项目 模型和文件数组。

    3 - 您必须编辑方法来保存数据。例如(在你的 case @item.save 不起作用,因为它不是设计用于回形针的 接受一个文件数组)。因此,在您的 Item 模型中,您将不得不 写一个类似下面的方法:

    (pseudo-code)
    
    def save(item_attributes, array_of_files)
     @item = Item.new( -- add here only Item attributes --)
     @item.save
    
     # Here you are creating a model file for each file you are saving.
     # So there will be N file model for 1 item.
     for file in array_of_files
       @file = File.new( @item.id, file)
       @file.save
     end
    
     # here you can implement transaction in case a error occurs. 
     # and shows the error to your client through Item model 
     # google how to do this (transaction and Active Record class).
    end
    

    【讨论】:

    • 谢谢@Pedro,实际上我是 Rails 新手,请指导我如何让处理程序同时保存多个文件?
    • 解决问题的最简单方法是每次保存一个文件。因此,您创建一个项目,而不是允许用户选择创建的项目来上传文件。用户可以重复这个过程 N 次。我说最简单,因为这就是回形针的设计方式,每个模型一个文件。关于同时保存多个文件,除了我给你看的链接之外,我无能为力。我以前从未这样做过,但据我从链接中阅读,它很好地解释了如何实现它!对不起。
    • google for "paperclip save multiple files" 有很多教程!
    • 我已经尝试过了,但无法理解主要问题。谢谢顺便说一句
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多