如何在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