2012-01-13 80 views
0

我很抱歉問一個初學者的問題。在特定情況下,我無法獲得「collection_select」到 的工作。Rails 3.1 collection_select通過兩個級別? (嵌套模型)

林正嘗試基於以下車型寫一個簡單的Rails 3.1的應用程序:

class Site < ActiveRecord::Base 
    has_many :supply_sites, :dependent => :destroy 
    has_many :demand_sites, :dependent => :destroy 
    accepts_nested_attributes_for :supply_sites 
    accepts_nested_attributes_for :demand_sites 
end 


class DemandSite < ActiveRecord::Base 
    belongs_to :site, :class_name => "Site" 
    has_many :translinks , :dependent => :destroy 
end 


class SupplySite < ActiveRecord::Base 
    belongs_to :site, :class_name => "Site" 
    has_many :translinks , :dependent => :destroy 
end 


class Translink < ActiveRecord::Base 
    belongs_to :supply_site, :class_name => "SupplySite" 
    belongs_to :demand_site, :class_name => "DemandSite" 
end 

遷移如下:

class CreateSites < ActiveRecord::Migration 
    def self.up 
    create_table :sites do |t| 
     t.string :name 
     t.string :codename, :limit => 3 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :sites 
    end 
end 



class CreateSupplySites < ActiveRecord::Migration 
    def self.up 
    create_table :supply_sites do |t| 
     t.integer :site_id 
     t.float :supply_quantity 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :supply_sites 
    end 
end 


class CreateDemandSites < ActiveRecord::Migration 
    def self.up 
    create_table :demand_sites do |t| 
     t.integer :site_id 
     t.float :demand_quantity 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :demand_sites 
    end 
end 


class CreateTranslinks < ActiveRecord::Migration 
    def self.up 
    create_table :translinks do |t| 
     t.integer :supply_site_id 
     t.integer :demand_site_id 
     t.float :unit_cost 
     t.float :quantity 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :translinks 
    end 
end 

我希望能夠添加一個新「的Translink 「 」供應站點「和」需求站點「之間的」(運輸鏈接)「,方法是根據 各個供應站點或需求站點的代碼名稱從下拉菜單中選擇,代碼名稱爲 r「網站」。

當添加了新的「供應點」(或「點播網站」),下面的作品以及 (從「_form.html.erb」對於任何一個供應點或需求的網站。

<div class="field"> 
    <%= f.label :site_id %><br /> 
    <%= f.collection_select :site_id, Site.find(:all), :id, :codename %> 
    </div> 

現在我想要添加一個新的「Translink」,用於連接 供應站點和需求站點。我不想分別手動添加 supply_site_id或demand_site_id,但所有供應站點的列表都選擇 在底層站點中定義的代碼名稱,以及 需求站點中的代碼名稱。我可以執行以下操作以執行drop-do WN菜單選擇,比方說,一走出現有的供應地點:

<div class="field"> 
    <%= f.label :supply_site_id %><br /> 
    <%= f.collection_select :supply_site_id, SupplySite.find(:all), :id, :id %> 
</div> 

但是,而不是顯示在下拉菜單中供網站的ID,我寧願看到從的代號選擇底層的「網站」。

<div class="field"> 
    <%= f.label :supply_site_id %><br /> 
    <%= f.collection_select :supply_site_id, SupplySite.find(:all), :id ,????? %> 
    </div> 

我該怎麼做?

任何幫助將不勝感激!

斯特凡

回答

0

檢查fields_for方法在formbuilder API。它允許你做這樣的事情來處理嵌套屬性:

<%= f.fields_for(options) do |nested_f| 
    nested_f.text_field :nested_attribute 
0

Thanks Katen!

的解決方案竟然是添加一個方法既需求廠類和供應點類,如下所示:

class SupplySite < ActiveRecord::Base 
    belongs_to :site, :class_name => "Site" 
    has_many :translinks , :dependent => :destroy 
    accepts_nested_attributes_for :translinks 


def supply_site_codename 
    self.site.codename 
end 

class DemandSite < ActiveRecord::Base 
    belongs_to :site, :class_name => "Site" 
    has_many :translinks , :dependent => :destroy 
    accepts_nested_attributes_for :translinks 


    def demand_site_codename 
    self.site.codename 
    end 

end 

這樣我可以使用標準「 collection_select「並引用:如下所示的基礎」站點「的代號:

<div class="field"> 
    <%= f.label :supply_site_id %> 
    <br/> 
    <%= f.collection_select :supply_site_id, SupplySite.find(:all), :id, supply_site_codename %> 
</div> 

<div class="field"> 
    <%= f.label :demand_site_id %> 
    <br/> 
    <%= f.collection_select :demand_site_id, DemandSite.find(:all), :id, :demand_site_codename %> 
</div> 

現在它可以按需要工作。關鍵是定義一個方法來引用相應的站點模型。

Stefan