Search code examples
ruby-on-railsformshelper

How to change html of the select form helper in rails?


When I used the select_tag helper, everything was working perfect with:

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

But I can't handle the select helper. Tried changing these ways:

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" %>

The result is always the same - it ignores html options(<select name="district_id" id="district_id">), however, options_from_collection_for_select work fine. What's the trick?


Solution

  • Try this as per the documentation here:

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

    Note, it takes the collection of districts as 2nd argument directly (not wrapped in options_from_collection_for_select).