parm530
1/2/2019 - 4:32 PM

Select Tag

Simple dropdown with selected value and object value set

Simple select dropdown (all are equivalent)

  • <select name="user_id" id="user_id"> </select>
  • <%= select_tag("user_id") %>
  • <%= select_tag(:user_id) %>

Add Options as 2nd Arg

// Generic way
<%= select_tag("user_id", "<option value='0'>Hi</option>".html_safe) %>

// More efficient way
<%= select_tag("user_id", options_for_select([[View, value], [View2, value2]]) ) %>

// Add the selected value of an object 
// the selected value has to match the value in the array
<%= select_tag("user_id", options_for_select([[View, value], [View2, value2]], @object.value) ) %>

// Add a data attribute
<%= select_tag("user_id", options_for_select([[View, value], [View2, value2]]), {"data-id" => @object.id } ) %>

// 
<%= select_tag "id-of-select", 
options_from_collection_for_select(array, "method1_to_call_as_value", "method2_to_call_as_what_to_display"),
class: "form-control" %>