2011-11-03 22 views
0

我是非常新的導軌開發。 我正在爲我的投資組合網站創建一個簡單的後端。2個型號在導軌3中的形式

我不確定這個問題的標題。我之前提出的一個問題可能過於複雜。所以我正在簡化它。

進出口使用3種型號:郵政,附件,Attachment_Category

我有我使用的一種形式:

  1. 草案後有標題,內容和類別。

  2. 顯示附件的類別在下拉(幻燈片,圖像,視頻)

  3. 上傳附件(多個)。

我已經實現步驟1和2

第3步:我希望它這樣,當我終於打提交表單時,attachment_category_id保存到附件表。

我有以下關係:

Post.rb

class Post < ActiveRecord::Base 

has_many :attachment_categories, :through => :attachments 
has_many :attachments,:dependent => :destroy 

accepts_nested_attributes_for :attachments 
validates_presence_of :title, :content, :category 

end 

Attachment.rb

class Attachment < ActiveRecord::Base 

belongs_to :post 
belongs_to :attachment_category 


#paperclip 
has_attached_file :photo, :styles =>{ 

:thumb => "100x100#", 
:small => "400x400>" 

} 

end 

Attachment_category.rb

class AttachmentCategory < ActiveRecord::Base 

has_many :posts , :through => :attachments 
has_many :attachments 

validates :category_name, :presence =>true 

end 

回答

0

所以我已經完成步驟1 , 部分 步驟2和步驟3.

使用我的解決方案,我只能上傳一個附件。 但它起作用:附件通過post_id和attachment_category_id保存到附件表中。

以下代碼來自_form.html.erb,它被髮送到post_controller.rb。 截取碼:

..... 

    <%= f.fields_for :attachments do |attach| %> <br> 

    <%= attach.collection_select :attachment_category_id, AttachmentCategory.all, :id, :category_name %> 
    <%= attach.file_field :photo %> <br> 

    <% end %> 

.....