2013-10-17 199 views
1

我有以下情況:Rails多態關聯has_one/belongs_to

我有一個名爲「ConfigurationItem」的模型。

class ConfigurationItem < ActiveRecord::Base 

    belongs_to :contract_asset 
    belongs_to :provider 

    belongs_to :configuration, polymorphic: true 


    validate :name, :contract_asset, presence: true 

end 

然後,我有暫時兩款車型,「OsConfiguration」和「HardwareConfiguration」

class OsConfiguration < ActiveRecord::Base 

    has_one :configuration_item, as: :configuration 

end 

class HardwareConfiguration < ActiveRecord::Base 

    has_one :configuration_item, as: :configuration 

end 

在我的創作過程中,我第一次來到形態項目的形式。所以我的問題是,我如何從ConfigurationItem表單創建一個Os或硬件配置。事情是這樣的:

enter image description here

我試了一下,到目前爲止是路線是這樣的:

resources :configuration_items do 
    resources :os_configurations 
    resources :hardware_configurations 
end 

但剩下的就是有點重,我(我很新的軌道)。

另外,我用這種寶石: https://github.com/codez/dry_crud

編輯:

更具體地講,從形態項目形式,我可以選擇一個操作系統或硬件配置。如果我選擇一個操作系統配置,一個模態形式將出現在他的表單中。當我保存Os配置時,我必須將他的屬性configuration_item設置爲先前的形式,所以他還沒有創建,我無法從os配置的控制器訪問它。

這就像在rails_admin中從表單創建並添加其他模型的新實例。

謝謝!

+0

看看我的答案在這裏:http://stackoverflow.com/a/17854938/2503775 – user2503775

回答

1

這裏是我的解決方案, 在我的形態項目的列表視圖,我加入了下拉菜單

%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)) 

在我ConfigurationItemsController,我創建下拉類型的配置。

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 

在我的形態項目的形式查看我向與我配置的場形式

- 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 

所以我我要哪個配置表單前進行選擇,而不是形式。

0

在您創建對象的configuration_items_controller中,檢查下拉輸入中的選擇,並根據設置的內容來創建該對象。

def create 
item = ConfigurationItem.new 
... do what you need to here ... 
item.save 
if (params[:dropdown]=='OS Configuration') 
    os_config = OSConfiguration.new 
    ... do what you need to ... 
    os_config.configuration_id = item.id 
    os_config.save 
elseif (params[:dropdown]=='Hardware Configuration') 
    hardware_config = HardwareConfiguration.new 
    ... do what you need to ... 
    hardware_config.configuration_id = item.id 
    hardware_config.save 
end 
end 
+0

抱歉,我遲到的答案。這個解決方案可以工作,但我沒有說,當我從下拉列表中點擊OsConfiguration時,我可以訪問他的表單。現在,我必須保存我的新OsConfiguration,然後我將返回到ConfigurationItem表單。我想從OsConfigurationController中設置他的configuration_item。但配置項目尚未創建... – brunettia

+0

爲什麼你不能同時使用這兩種表單,並且只能根據用戶選擇的下拉菜單顯示錶單? – lilwupster

+0

是的,這實際上是我做的。我會發布我的解決方案 – brunettia