Here is my solution, In my ConfigurationItem's list view, I added the dropdown menu
%ul.dropdown-menu.pull-right
- ConfigurationItemsController::ITEM_TYPES.keys.each do |type|
%li= link_to("Add #{type.titleize} Item", new_contract_contract_asset_configuration_item_path(@contract, @contract_asset, type: type))
In my ConfigurationItemsController, I create the configuration with the type of the dropdown.
ITEM_TYPES = { 'plain' => nil,
'os' => OsConfiguration,
'hardware' => HardwareConfiguration }
before_filter :assign_configuration_type, only: [:new, :create]
def assign_configuration_type
if type = ITEM_TYPES[params[:type]]
entry.configuration = type.new
end
end
def models_label(plural = true)
if @configuration_item
if config = @configuration_item.configuration
"#{config.class.model_name.human.titleize} Item"
else
"Plain Configuration Item"
end
else
super(plural)
end
end
In my ConfigurationItem's form view I extend the form with my configuration's field
- if entry.new_record?
= hidden_field_tag :type, params[:type]
- if @configuration_item.configuration
= f.fields_for(:configuration) do |fields|
= render "#{@configuration_item.configuration.class.model_name.plural}/fields", f: fields
So I choose before the form which configuration I'll have, and not in the form.