如何在rails中更改选择表单助手的html?

时间:2018-05-16 10:51:45

标签: ruby-on-rails forms helper

当我使用select_tag帮助程序时,所有内容都完美地用于:

<%= select_tag :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), { class: "select", id: "district" } %>

但我无法处理选择助手。尝试改变这些方式:

1)<%= form.select :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), html_options: { class: "select_inside_col", id: "locality" } %>

2)<%= form.select :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), html: { class: "select_inside_col", id: "locality" } %>

3)<%= form.select :district_id, options_from_collection_for_select(districts, "id", "title", prompt: "All"), class: "select_inside_col", id: "locality" %>

结果始终相同 - 它忽略了html选项(<select name="district_id" id="district_id">),但是,options_from_collection_for_select工作正常。 有什么诀窍?

1 个答案:

答案 0 :(得分:0)

根据文档here尝试此操作:

<%= form.select :district_id, districts.map { |d| [d.title, d.id] }, { include_blank: "All" }, { class: "select_inside_col", id: "locality" } %>

注意,它直接将区域集合作为第二个参数(不包含在options_from_collection_for_select中)。