2011-11-03 52 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 %> 

.....