問題は、これらのクラスがあることです。
class Post < ActiveRecord::Base
belongs_to :category
has_many :text_fields
has_many :integer_fields
accepts_nested_attributes_for :text_fields
accepts_nested_attributes_for :integer_fields
end
class Category < ActiveRecord::Base
has_many :fields
end
class TextField < ActiveRecord::Base
belongs_to :post
end
class IntegerField < ActiveRecord::Base
belongs_to :post
end
Category
には多くの があり、すべてのフィールド (またはFields
と混同しないでください) には列 " " (テキスト フィールドか整数フィールドかを指定するため) と説明 (整数フィールドの場合は「ペットを何匹飼っていますか」など) があります。ユーザーが新しい投稿を作成する場合、最初にカテゴリを選択します。次に、各カテゴリの内容に基づいて、適切であり、ビルドおよびレンダリングする必要があります。私の要点が明確であることを願っています。カテゴリーに基づいてさまざまなフィールドを生成するポストジェネレーターが必要です。TextField
IntegerField
type
Fields
TextFields
IntegerFields
投稿フォーム (今のところステップなし)
= simple_form_for @poster do |f|
= f.input :category_id, collection: Category.all, prompt: "Choose category"
= f.input :title
= f.simple_fields_for :text_fields do |ftf|
= ftf.input :description
= f.simple_fields_for :integer_fields do |fif|
= fif.input :integer_number
= f.submit
そして、ここが間違っています。各フィールドに適切なプロンプトを表示する方法がわかりません。各 TextField または IntegerField はCategory
、(フィールドに基づいて) 必要なためだけに存在します。integer number
しかし、毎回「 」だけでなく、適切なプロンプトを表示することはできますか?
これらのフィールドを作成する方法は次のとおりです。変更が可能です。
def build_fields_based_on_category
category.fields.each do |field|
self.send("#{field.kind}_fields").build
##### 'kind' can be integer or 'text'
end
end