【发布时间】: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
= 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