select tag . Default value rails
<%= select_tag(:city, options_for_select([['New York', 1], ['San Francisco', 2], ['Seattle', 3]], params[:city]), prompt: 'Select a city') %>
Explanation:
<%= ... %>
: Embeds Ruby code into the HTML ERB file for execution.select_tag(:city, ...)
:select_tag
: Generates an HTML select dropdown element.:city
: Symbol used as the name for the select dropdown.options_for_select([['New York', 1], ['San Francisco', 2], ['Seattle', 3]], params[:city])
:options_for_select
: Constructs the options for the select dropdown.[['New York', 1], ['San Francisco', 2], ['Seattle', 3]]
: Array of arrays containing city names and corresponding values.params[:city]
: Sets the selected option based on the value ofparams[:city]
.prompt: 'Select a city'
:prompt
: Adds a default prompt option to the dropdown with the specified text 'Select a city'. ```