(注:我已閱讀http://shiningthrough.co.uk/Select-helper-methods-in-Ruby-on-Rails,但仍然不知道選擇什麼樣的)使用collection_select或選擇或select_tag
我將要(該方法獲得)作爲搜索過濾器的形式。
<%= form_tag(reports_courses_allocated_path, :method =>'get') %>
<%= collection_select(:course, :id, @courses, :id, :title, :include_blank => "All Courses") %>
</form>
上面產生欲用於標籤的輸出,但沒有「選擇」參數,我可以通過在一個PARAMS值。我也不喜歡這樣的事實,這個名字當然是[ID],我寧願它命名COURSE_ID
我可以這樣做:
<%= select_tag(:course_id, raw("<option>All Courses</option>") + options_from_collection_for_select(@courses, :id, :title, params[:course_id])) %>
但我不喜歡我的事實連接選項,但這可能是我最好的選擇。
有沒有意見/建議?