2011-06-25 12 views
4

Postbelongs_toSectionというモデルがあります。 2つの異なるサブクラスがあり、それぞれに対して異なる動作を実装するためにSTIを使用します。 Postフォームには、それぞれSectionのタブがあります。タブでは、A)既存のSectionから選択するか、<select>を使用するか、またはB)ユーザーに新しいSectionを作成させます。 accepts_nested_attributes_forfields_forを使用する方法、またはこれを行うために必要なものを知りたい場合The Rails Way単一テーブル継承でaccepts_nested_attributes_forを使用する

アドバイスをいただければ幸いです。ありがとう。タブを想定し

+0

コードを入力できますか? – Dinatih

答えて

0

はあなたが道の85%を取得する必要があります2つのサブクラス

class Post 
    # the two subclasses. Each instance will only be using one or the other 
    belongs_to :section_foo 
    belongs_to :section_bar 

    accepts_nested_attributes_for :section_foo 
    accepts_nested_attributes_for :section_bar 
end 

とビューで(おそらく一度タブあたり)

= form_for @post do |f| 
    = f.select :section_id, SectionFoo.all # etc 
    = fields_for @post.build_section_foo do |s| 
    = s.text_field :bla_bla_bla 

に対応しています。古いセクションを割り当てる新しいセクションの作成を避けるために、accept__ *にreject_if biddingが必要な場合があります。

関連する問題