【问题标题】:Rails 3 STI model inside nested form嵌套形式内的 Rails 3 STI 模型
【发布时间】:2014-04-23 14:22:13
【问题描述】:

我正在尝试在 Rails 3 中构建一个 CRUD 控制器和表单。

我有

class Publication < ActiveRecord::Base

  has_many :posts

end

其中 Posts 是 STI 模型:

class Post < ActiveRecord::Based
  attr_accessible :title, :description
end

我有几个继承的模型:

class Image < Post
end

class Video < Post
end

class Status < Post
end

等等

我想为发布创建一个 CRUD,用户可以在其中添加任意数量的帖子,为任何类型的帖子动态添加嵌套表单。

有没有我可以使用的 gem 来支持这种带有 STI 的嵌套表单?

我尝试构建一个表单,但我需要修改 Publication 类并为每个附加的继承模型引入嵌套属性。有没有办法避免这样做?

class Publication < ActiveRecord::Base

  has_many :videos, :dependent => :destroy
  accepts_nested_attributes_for :videos, allow_destroy: true
  attr_accessible :videos_attributes

  has_many :posts

end

【问题讨论】:

    标签: ruby-on-rails-3 nested-forms sti


    【解决方案1】:

    你可以这样做。

    在您的发布控制器中

    class PublicationsController < ApplicationController
        def new
           @publication = Publication.new
           @publication.build_post
        end
    end
    

    你的模型应该是这样的

    class Publication < ActiveRecord::Base
        has_many :posts, dependent: :destroy
        accepts_nested_attributes_for :posts    
    end
    
    class Post < ActiveRecord::Base
        belongs_to :publication
        Post_options = ["Image", "Video", "Status"]
    end
    

    在你的表单中

    <%= form_for(@publication) do |f| %>
      <p>
        <%= f.label :title %><br>
        <%= f.text_field :title %>
      </p>
    
      <p>
        <%= f.label :description %><br>
        <%= f.text_area :description %>
      </p>
    
       <%= f.fields_for :post do |p| %>
          <%= p.label :post_type %>
          <%= p.select(:post_type, Post::Post_options , {:prompt => "Select"}, {class: "post"}) %>
       <% end %>
      <p>
        <%= f.submit %>
      </p>
    <% end %>
    

    注意:您应该在您的 Post 模型中拥有一个 post_type 属性才能使其正常工作。

    【讨论】:

    【解决方案2】:

    我写了一篇关于这个问题的简短博客文章:http://www.powpark.com/blog/programming/2014/05/07/rails_nested_forms_for_single_table_inheritance_associations

    我基本上决定使用cocoon gem,它提供了两个辅助方法-link_to_add_associationlink_to_remove_association,它们将各自的装饰类字段动态添加到表单中(例如PostImageVideo

    # _form.html.haml
    
    = simple_form_for @publication, :html => { :multipart => true } do |f|
    
      = f.simple_fields_for :items do |item|
        = render 'item_fields', :f => item
      = link_to_add_association 'Add a Post', f, :items, :wrap_object => Proc.new { |item| item = Item.new }
      = link_to_add_association 'Add an Image', f, :items, :wrap_object => Proc.new { |item| item = Image.new }
      = link_to_add_association 'Add a Video', f, :items, :wrap_object => Proc.new { |item| item = Video.new }
    
      = f.button :submit, :disable_with => 'Please wait ...', :class => "btn btn-primary", :value => 'Save'
    

    :wrap_object proc 生成正确的对象,在_item_fields partial 中呈现,例如:

    # _item_fields.html.haml
    
    - if f.object.type == 'Video'
      = render 'video_fields', :f => f
    - elsif f.object.type == 'Image'
      = render 'image_fields', :f => f
    - elsif f.object.type == 'Post'
      = render 'post_fields', :f => f
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多