Add it to your Gemfile:
gem 'dynamic_simple_form'
Run the following command to install it:
bundle install
Run the simple_form generator:
rails generate simple_form:install
Add {model}_type_id to your model OR create new your model with {model}_type_id.
rails generate migration AddProductTypeIdToProducts product_type_id:references
OR
rails generate migration CreateProduct product_type_id:references [and your columns...]
Declare dynamic_simple_form in your model.
class Product < ActiveRecord::Base
# dynamic_simple_form create association automatically.
# CustomType has many CustomFields
# Product belongs to CustomType
# Product has many FieldValues as values
dynamic_simple_form type_class: CustomType, # default => ProductType
field_class: CustomField # default => ProductField
value_class: FieldValue # default => ProductFieldValue
type_dependent: :nullify # default => :destroy
# Model has also static fields
validates :name, presence: true
validates :price, numericality: { only_integer: true }
end
Run the dynamic_simple_form generator
rails generate dynamic_simple_form product description:text sale:boolean
Mount Engine
mount DynamicSimpleForm::Engine => '/dynamic_simple_form'
- Ruby >= 1.9
- Rails >= 3
- SimpleForm >= 3.0.0.beta1
- create engine
- write lib/generators/USAGE
- support check_boxes