Ruby on rails 3 rails 3中的2个模型

Ruby on rails 3 rails 3中的2个模型,ruby-on-rails-3,forms,model-associations,Ruby On Rails 3,Forms,Model Associations,我对rails开发非常陌生。 我正在为我的投资组合网站创建一个简单的后端 我不确定这个问题的题目。我之前问的一个问题可能太复杂了。所以我把它简化了 即时通讯使用3种模式:帖子、附件、附件类别 我有一个表格,用于: 用标题、内容和类别起草文章 在下拉列表中显示附件类别(幻灯片、图像、视频) 上载附件 我已经实施了步骤1和步骤2 对于步骤3:我希望这样,当我最终点击表单上的提交时,附件类别id会保存到附件表中 我有以下关系: Post.rb class Post < ActiveRecord:

我对rails开发非常陌生。 我正在为我的投资组合网站创建一个简单的后端

我不确定这个问题的题目。我之前问的一个问题可能太复杂了。所以我把它简化了

即时通讯使用3种模式:帖子、附件、附件类别

我有一个表格,用于:

  • 用标题、内容和类别起草文章

  • 在下拉列表中显示附件类别(幻灯片、图像、视频)

  • 上载附件

  • 我已经实施了步骤1和步骤2

    对于步骤3:我希望这样,当我最终点击表单上的提交时,附件类别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
    
    class Post:附件
    有多个:附件,:依赖=>:销毁
    接受附件的\u嵌套\u属性\u
    验证是否存在:title、:content、:category
    结束
    
    附件.rb

    class Attachment < ActiveRecord::Base
    
    belongs_to :post
    belongs_to :attachment_category
    
    
    #paperclip
    has_attached_file :photo, :styles =>{
    
    :thumb => "100x100#",
    :small => "400x400>"
    
    }
    
    end
    
    类附件{
    :thumb=>“100x100#”,
    :small=>“400x400>”
    }
    结束
    
    附件_category.rb

    class AttachmentCategory < ActiveRecord::Base
    
    has_many :posts , :through => :attachments
    has_many :attachments
    
    validates :category_name, :presence =>true
    
    end
    
    class AttachmentCategory:附件
    有很多附件
    验证:category_name,:presence=>true
    结束
    
    因此,我已经完成了步骤1、步骤2和步骤3的部分内容

    使用我的解决方案,我可以只上传一个附件。 但它是有效的:附件将使用post_id和attachment_category_id保存到Attachments表中

    下面的代码来自_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 %>
    
    .....
    
    。。。。。
    

    .....