我試圖讓ActionView-Helper collection_select
獲取將在下拉菜單中預選的值。Rails:在ActionView-Helper'collection_select'中預選一個值
無論(:selected
在HTML選項哈希)
<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {}, {:selected => @my_collection_object.id}) %>
(在選項哈希
:selected
)
也不
<%= collection_select(:my_object, :my_method, @my_collection, :id, :description_string, {:selected => @my_collection_object.id}, {}) %>
似乎工作。
我在做什麼錯?任何人都可以幫我解決這個問題嗎?
我接受了這個答案,但我只是注意到,我不得不從collection_select切換到選擇。我有兩個收集選擇,以選擇兩個不同的「職位。author_id「(使用上面的例子),所以我可以在頁面上比較它們。我在每個collection_select上都有一個javascript-observer,它不起作用,因爲collection_selects總是輸出下面的html-element-id:post_author_id。不起作用,因爲兩個collection_select的html-element-id是相同的 – Javier 2009-06-22 09:03:17
是的,Rails不保證唯一元素ID的事實有點問題 – 2009-06-30 19:11:21