2013-02-07 94 views
1

我有一個表單,允許您按名稱選擇一個設施,然後將設施的編號寫入數據庫中的值。選擇連接字段名稱

<%= f.collection_select(:transfer_to_id, Facility.all, :id, :facility_name, {:include_blank => true}, {:class => 'select'})%> 

我希望能夠選擇的設施名稱,但該設施名稱的右側顯示的形式facility_address。我不知道如何做到這一點,可能是某種數組或使用輔助方法。

如果任何人都可以提供一些幫助,將不勝感激。

+0

如果我理解正確,您想在選擇框中連接設施的名稱和地址?例如'設施X - 123 Abc Street'? – Noz

+0

沒錯。我想出了這個,但不知道是否正確。 'def facility_select Facility.order(「facility_name」)。map {| facility | [「#{facility.facility_name}#{facility.facility_address}」,facility.id]} end' – nulltek

回答

2

這是什麼結束了正常工作,我通過創建類滿足HOD。

def facility_name_with_facility_address 
    "#{facility_name} | #{facility_address}" 
    end 
2

這是未經測試如此忍受我,但你需要一個方法添加到您的Facility型號:

def facility_name_with_facility_address 
    facility_name << " " << facility_address 
end 

,然後在表格要更改如下:

<%= f.collection_select(:transfer_to_id, Facility.all, :id, :facility_name, {:include_blank => true}, {:class => 'select'})%> 

對於這個:

<%= f.collection_select(:transfer_to_id, Facility.all, :id, :facility_name_with_facility_address, {:include_blank => true}, {:class => 'select'})%> 
+1

適合我!謝謝 – MSC