我想上傳一個表單上多個文件。我遵循軌道鑄造回形針和嵌套屬性,也是本教程http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/,但我似乎無法得到它的工作...回形針和嵌套屬性 - 不寫入數據庫
我也在這裏搜索堆棧溢出,看了所有的回形針和嵌套屬性職位,但我似乎無法找到我的答案,看來我做的一切權利......
會發生什麼事是,當我提交表單,它創建的廣告(這是一個廣告程序),它說一切正常,但它不會將圖像數據寫入數據庫,也不會上傳文件...
所以我有分類模型:
class Classified < ActiveRecord::Base
has_many :classified_images, :dependent => :destroy
accepts_nested_attributes_for :classified_images, :reject_if => lambda { |t| t['classified_image'].blank? }
attr_accessible :classified_images_attributes, :access, :contact, :price, :bizType
end
然後,Classified_Image型號:
class ClassifiedImage < ActiveRecord::Base
belongs_to :classified
has_attached_file :photo, :styles => {:small => "150x150>", :large => "320x240>"},
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/classifieds/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
attr_accessible :caption, :photo
end
在分類控制,對 「新」 的一部分,我有: 高清新 @Classified = Classified.new
3.times { @classified.classified_images.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @classified }
end
end
在「_form」上我有:
<%= form_for @classified, :html => { :multipart => true } do |f| %>
...
<%= f.fields_for :classified_images do |builder| %>
<%= render 'image_fields', :f => builder %>
<% end %>
在 「image_fields」 偏我有:
<% if f.object.new_record? %>
<li>
<%= f.label :caption %>
<%= f.text_field :caption %>
<%= f.label :photo %>
<%= f.file_field :photo %>
</li>
<% end %>
在遷移文件我有:
class AddAttachmentPhotoToClassifiedImages < ActiveRecord::Migration
def self.up
add_attachment :caption, :classified_id, :photo
end
def self.down
drop_attached_file :caption, :classified_id, :photo
end
end
class CreateClassifiedImages < ActiveRecord::Migration
def change
create_table :classified_images do |t|
t.string :caption
t.integer :classified_id
t.timestamps
end
end
end
在 「development.rb」 文件我有:
Paperclip.options[:command_path] = "/usr/local/bin/"
Paperclip.options[:log] = true
這裏的日誌的一個例子,當我提交形式:
開始POST 「/ classifieds」爲127.0.0.1於2013-05-19 23:39:43 +0100 通過ClassifiedsController處理#以HTML格式創建 參數:{「utf8」=>「✓」,「authenticity_token」=>「978KGJSUlmMEvr6Tysg5xYIEQzNLn5vod07g + Z7njkU = 「 」分類「=> { 」接觸「=> 」918218338「, 」價格「=> 」1500「, 」訪問「=> 」BONS「, 」classified_images_attributes「=> { 」0「=> {」 caption「=>」teste「,」photo「=>#@ original_filename =」064_dont-count-the-days.jpg「,@ content_type =」image/jpeg「,> @ headers =」Content-Disposition:form-data ;名= \ 「分類[classified_images_attributes] [0] [照片] \」; filename = \「064_dont-count-the-days.jpg \」\ r \ nContent-Type:image/jpeg \ r \ n「,> @ tempfile =#3954-11t04t >>},」1「=> {」 (0.1ms)開始交易 SQL(0.5ms)=>「>」>},「2」=> {「caption」=>「」 INSERT INTO「classifieds」(「access」,「contact」,「created_at」,「price」,)> VALUES(?,?,?,?)[[「access」,「bons」],[「contact」 2013年5月19日星期日22:39:15 UTC +00:00] 43 UTC> 00:00]] (爲0.8ms)提交重定向到本地主機事務 :3000 /分類/ 8 完成302在5ms的實測值(ActiveRecord的:1.4ms之)
正如你可以看到,它插入「classifi編輯「表,但不進入」classifieds_image「表,並且,我沒有從回形針獲得任何信息...
對不起所有的代碼,但這應該是真的很簡單,我沒有看到,作爲更多的信息,你有更好的幫助我...請讓我知道,如果你需要更多的代碼或信息...
謝謝,它的工作,這是一個簡單的細節:)現在我有麻煩顯示照片,但這是我解決的另一個問題,謝謝期待你的答覆! – lbramos