2012-04-20 30 views
3

我正在使用best_in_place gem編輯inline列表和country_select以呈現可供選擇的國家列表。當使用best_in_place編輯選擇字段我這樣做:如何將country_select gem與best_in_place編輯

<%= best_in_place(@home, :country_name, :type => :select, :collection => [[1, "Spain"], [2, "Italy"]]) %> 

現在我希望得到所有country_select擁有並傳遞到收集參數的國家名單。 的country_select寶石提供了一個簡單的輔助渲染選擇欄:

<%= country_select("home", "country_name") %> 

我想更換:在best_in_place助手收集參數包括由country_select提供的國家名單。我知道best_in_place希望將[[key,value],[key,value],...]輸入到:collection中,但我不確定如何執行此操作。請指教。由於

回答

5

只要做到以下,也將努力:

<%= best_in_place @home, :country, type: :select, collection: (ActionView::Helpers::FormOptionsHelper::COUNTRIES.zip(ActionView::Helpers::FormOptionsHelper::COUNTRIES)) %> 
+0

感謝,它的工作原理就像一個魅力。如果可能的話,你可以解釋收集部分的東西,我不明白它爲什麼起作用。 – Hishalv 2012-06-15 11:05:17

+2

Country Gem位於ActionView :: Helpers內部。如果你檢查那個gem的代碼,你會發現它包含在ActionView,Helpers和FormOptionsHelper中。所以,如果你想訪問COUNTRIES常數,你需要添加所有的API。 zip方法是Array的一種方法。它只是將前一個元素添加到數組中。 – 2012-06-17 23:08:59

0

如果你是4幾年後使用導軌,這樣做的伎倆:

<%= best_in_place @cart.order, :country_name, type: :select, :collection => ActionView::Helpers::FormOptionsHelper::COUNTRIES%> 
相關問題