如何在Paperclip,ActiveAdmin和Formtastic中创建表单?图像不是模型的一部分,而是子模型的一部分。Ruby,Paperclip,Formtastic,ActiveAdmin和图像
我想:
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"
我试过了,但得到这个错误:'TicketImage(#83482896)预计,得到的ActiveSupport :: HashWithIndifferentAccess(#20656416)'。另外,只有一个照片字段出现。我也必须在每个块之后添加'end'。 – Chloe 2013-03-04 01:34:41
嗨,我认为Ticket has_many TicketImage,不是belongs_to – why 2013-03-04 02:25:45
Naa,Rails倒退了。我所有的其他关系都是belongs_to(这实际上意味着has_one)。我尝试将其更改为has_one,然后查看表单,并与错误消失:未知列'ticket_images.ticket_id'。 – Chloe 2013-03-04 02:32:43