我爲我的下拉列表:如何在Rails中設置selected_key grouped_options_for_select?
<%= select_tag 'daily_order[kitchen_id]', grouped_options_for_select(Kitchen.for_normal_and_atworld_kitchens_select, selected_key = :id, options = {}), class: "form-control", onchange: "kitchen_change()" %>
但selected_key
當我通過了:id
不工作。
當我硬編碼它是這樣的:
<%= select_tag 'daily_order[kitchen_id]', grouped_options_for_select(Kitchen.for_normal_and_atworld_kitchens_select, selected_key = 5, options = {}), class: "form-control", onchange: "kitchen_change()" %>
它的正常工作。
我的廚房模型:
def self.for_normal_and_atworld_kitchens_select
{
'Normal Kitchens' => where(special_kitchen: false, atworld: false).where.not(id: @exclude_kitchens).map {|x| [x.name, x.id]},
'Around The World Kitchens' => where(special_kitchen: false, atworld: true).where.not(id: @exclude_kitchens).map {|x| [x.name, x.id]}
}
end
我現在應該做的,使這個grouped_options_for_select SELECTED_KEY工作?
注:
@exclude_kitchens
是廚房ID的像陣列[1,2,3]。它會返回正確的輸出並在項目內的其他地方正常工作。