【问题标题】:No handler found error with a nested paperclip没有处理程序发现嵌套回形针错误
【发布时间】:2014-07-27 11:27:45
【问题描述】:

我正在开发一个需要使用回形针上传图片的网络应用程序。为了避免出现“No handler found for picture_name”错误,我一直使用 form_for 助手来创建包含图片的 file_field。

现在,我需要上传一张图片,但这张图片在另一个带有“accept_nested_attributes_for”的模型中。为了避免嵌套模型出现上述错误,我需要使用初始 form_for 中的 fields_for 帮助器。但是我需要动态创建嵌套模型的新实例。为此,我总是要求查看嵌套模型的空实例并将其附加到表单中。但是,我不能在 html GET 请求中使用 form_for 作为参数,所以如果我避免使用 fields_for,我会再次出现“No handler found for picture_name”错误。

对于此要求是否有其他方法或可能的解决方案?我已经在网上冲浪了几个小时,但找不到任何可能的解决方案。

这是我使用的主要文件:

app/models/how_to.rb(父模型)

class HowTo
  include Mongoid::Document
  include Mongoid::Paperclip
  ...
  # Relations
  has_many   :transports
  accepts_nested_attributes_for :transports, allow_destroy: true
  ...
end

app/models/transport.rb(嵌套模型)

class Transport
  include Mongoid::Document
  include Mongoid::Paperclip
  ...
  has_mongoid_attached_file :picture,
    default_url: '/pictures/template.jpg',
    styles: {large: ['640x160'], small: ['300x300>']}, size: { in: 0..3.megabytes },
    content_type: [ "image/jpg", "image/png", "image/bmp" ], storage: :filesystem,
    path: ':rails_root/public/pictures/:id/:style.:extension',
    url: '/pictures/:id/:style.:extension'

  validates_attachment_content_type :picture,
    :content_type => ["image/jpg", "image/jpeg", "image/png", "image/bmp"]
  ...
end

app/controllers/transports_controller.rb

class TransportsController < ApplicationController
  ...
  def new_transport_partial
    respond_to do |format|
      format.js {
        render partial: ".../transport", layout: false,
        locals: { transport: Transport.new, number: params[:number].to_i }
      }
    end
  end
  ...
end

app/view/.../how_to.html.haml

%div{ id: "how_to#{ how_to.id }" }
  = form_for how_to, url: how_to_path( how_to ), html: { method: :put } do |f|
    ...
    %a.add_transport_button.upload_button.green_button{ rel: how_to.id, href: '#' }
      %i.fa.fa-plus
      %span
        = "ADD..."

    - how_to.transports.each_with_index do |transport, index|
      %div
        &nbsp;
      = render 'transport', transport: transport, number: index, f: f

    = f.submit "Submit", class: 'submit_how_to', rel: how_to.id
    ...

app/view/.../transport.html.haml

%div.transport_partial{ rel: transport.id }
  - if transport.persisted?
    = hidden_field_tag :id, transport.id, name: 'how_to[transports_attributes][][id]'
  = hidden_field_tag :_destroy, transport ? transport.attributes['_destroy'] : false, 
    disabled: true, id: "transport_destroy_#{ transport.id }", name: 'how_to[transports_attributes][][_destroy]'
  = text_field_tag :title, transport.title, size: 20,
    id: "transport_title_#{ transport.id}", name: 'how_to[transports_attributes][][title]'
  = hidden_field_tag :number, value: number, id: "transport_number_#{ transport.id}", 
    name: 'how_to[transports_attributes][][number]'
  = image_tag transport.picture.url(:small), id: "original_picture_#{ transport.id }_#{ number }",
    class: 'transport_picture transport_preview_picture', rel: transport.id
  = image_tag nil, id: "preview_picture_#{ transport.id }_#{ number }",
    class: 'transport_picture transport_preview_picture', rel: transport.id
  = file_field_tag :picture, value: transport.picture.url, id: "selected_file_info_#{ transport.id }_#{ number }",
    class: 'selected_file_transport', size: '24', name: "how_to[transports_attributes][][picture]",
    multiple: true, transport_id: transport.id, rel: number,
    onchange: "readURLprofile(this, 'transport', '#{ transport.id }_#{ number }');", style: 'display:none'

  %input.upload_picture.transport_upload.transport_picture_upload{ type: "button",
    onclick: "$(this).parent().find('#selected_file_info_#{ transport.id }_#{ number }').trigger('click')",
    transport_id: transport.id, rel: number, value: "UPLOAD" }    
  %a.transport_remove.upload_button.red_button{ rel: transport.id, href: '#' }
    %i.fa.fa-times
    %span
      = "REMOVE"

提前致谢。

PD:如果您需要更多代码,请告诉我。

【问题讨论】:

  • 汪!这是一个丑陋的代码块。 :)
  • 不过,我也在努力解决同样的问题,所以如果我找到解决方案,我会告诉你。
  • 我需要空间,所以我已经回答了你。

标签: ruby-on-rails mongoid paperclip nested-attributes


【解决方案1】:

在考虑了很多程序之后,我终于找到了一个有点复杂和肮脏的解决方案,但它是唯一适合我目的的方法。

正如我在问题中所说,我有 3 个模型:HowToTransportLocation。 HowTo 是 Transport 的父级,Transport 是 Location 的父级。 Aso,HowTo 接受 Transport 的嵌套属性,Transport 接受 Location 的嵌套属性。 Transport 是唯一使用 Paperclip 具有图像的模型,我在使用 fields_for 时遇到问题,并且出现“No handler found for picture_name”错误。

为了避免这些问题,我决定使用Remotipart gem,它允许您提交表单而无需使用 jQuery 文件上传重新加载页面。这样,我就有了 HowTo 实例的大表单和 Transport 实例的不同表单,它们独立于 HowTo 模型的大表单。 Transport 实例的这些形式包含 Location 实例的嵌套属性,然后使用 remotipart 提交。在 javascript 中,我手动提交不同的传输表单,最后我继续使用大的 HowTo 表单。

PD:如果您需要代码,请告诉我,我会发布。

【讨论】:

  • 酷,感谢您的跟进。我会看看 Remotipart,看看它是否符合要求。干杯。
猜你喜欢
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 2013-12-09
  • 2023-03-11
相关资源
最近更新 更多