0

如何在 Paperclip、ActiveAdmin 和 Formtastic 中创建表单?图像不是模型的一部分,而是子模型的一部分。

我试过:

form :html => {:multipart => true} do |f|
   f.inputs "Ticket Info" do
      ...
      f.input :image1.photo
      f.input :image2.photo

但它给出了一个错误ActionView::Template::Error (undefined method 'photo' for :image1:Symbol):

这是票证模型:

class Ticket < ActiveRecord::Base
  attr_accessible ... :image1_id, :image2_id
...
  belongs_to :image1, class_name: "TicketImage"
  belongs_to :image2, class_name: "TicketImage"

这是 TicketImage 模型:

class TicketImage < ActiveRecord::Base
  attr_accessible :file, :photo
  has_one :ticket
  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
end

我也试过f.input :image1,但它只是给了我一个空的选择框。


我也试过f.input :image1, :as => :file了,但是在我选择一个文件并单击提交后它给了我这个错误:

ActiveRecord::AssociationTypeMismatch in Admin::TicketsController#update
TicketImage(#89768376) expected, got ActionDispatch::Http::UploadedFile(#29533068)

我也试过

  f.semantic_fields_for :image1 do |image|   
        image.input :photo, :as => :file, :name => "Image1"
  end
  f.semantic_fields_for :image2 do |image|   
        image.input :photo, :as => :file, :name => "Image2"
  end

但它只给出了一个标有照片的文件选择按钮,在我提交后,出现了这个错误:

ActiveRecord::AssociationTypeMismatch in Admin::TicketsController#update
TicketImage(#86546820) expected, got ActiveSupport::HashWithIndifferentAccess(#20656416)

我也试过这个:添加:

ActiveAdmin.register Ticket do
    ...
    f.input :photo, :as => :file, :for => :image1, :name => "Image 1"

class Ticket < ActiveRecord::Base
  attr_accessible ... :image1_id, :image2_id, :image1_attributes, ...
  accepts_nested_attributes_for :image1, :image2, :allow_destroy => true


class TicketImage < ActiveRecord::Base
  attr_accessible :file, :photo, :photo_file_name, :photo_content_type, :photo_file_size, :photo_updated_at

并且文件上传框出现并接受上传,但在日志中,它说:

WARNING: Can't mass-assign protected attributes: photo

我也试过这个,它适用于一个上传字段,但是当两者都放在表单中时,都不显示!服务器日志中也没有错误:

  f.semantic_fields_for :image1 do |image|
    image.input :photo, :as => :file, :name => "Image1", :hint => image.object.nil? ? "No Image" : f.template.image_tag(image.object.photo.url(:thumb))
  end
  # f.semantic_fields_for :image2 do |image|   
    # image.input :photo, :as => :file, :name => "Image2", :hint => image.object.nil? ? "No Image" : f.template.image_tag(image.object.photo.url(:thumb))
  # end

我也试过这个,它似乎一直有效并保存图像,除了它允许您创建与 image1 字段的一对多关联,这只是一个条目!我没有尝试将两个图像添加到 image1,但我希望它会崩溃。

  f.has_many :image1  do |p|
    p.input :photo, :as => :file, :label => "Image 1", :hint => p.template.image_tag(p.object.photo.url(:thumb)) 
    p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
  end
  f.has_many :image2  do |p|
    p.input :photo, :as => :file, :label => "Image 2", :hint => p.template.image_tag(p.object.photo.url(:thumb)) 
    p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
  end

这效果最好,但提示不起作用!

  f.inputs :photo, :as => :file, :for => :image1, :name => "Image 1", :hint => "A Hint"
  f.inputs :photo, :as => :file, :for => :image2, :name => "Image 2", :hint => "A Hint"
4

1 回答 1

1

Formtastic 也支持嵌套表单:

form :html => {:multipart => true} do |f|
  f.input :number
  f.semantic_fields_for :image1 do |image|   
    image.input :photo, :name => "Image1"
  f.semantic_fields_for :image2 do |image|   
    image.input :photo, :name => "Image2"
于 2013-03-04T01:21:01.723 回答