Ruby saving selected choice in select_tag
I currently have a form that will pass 2 parameters to my controller. My question is every time I make a choice in 开发者_如何学JAVAthe select_tag form, I want my option to stay after I hit the submit tag. That way the user knows what he or she just selected. I could used :selected=>"true", but thats only for the default value and not for the value submitted.
<form name="filter" action="" style="display:inline" >
<label for="filter">Filter by Name or Description: </label>
<%= text_field_tag "query", params['query'] %>
<label for="status">Filter by Status:</label>
<%= select_tag(:sortstatus,
'<option value="empty">Show All</option>,
<option value="0">Applying</option>,
<option value="3">Suspended</option>,
<option value="4">Pending</option>') %>
<%= submit_tag 'Search' %>
</form>
And here is the controller that will change the value of empty to work with my table
def sort_status
if params[:sortstatus] == "empty"
@statusorder = ""
else @statusorder = params[:sortstatus]
end
end
Haven't been able to find any solution so far in Google.
Take a look at using options_for_select to generate your options
tags. It allows you to specify which entry you would like to be selected. e.g.
<%= select_tag(:sortstatus, options_for_select([['Show All', 'empty'],
['Applying', '0'],
['Suspended', '3'],
['Pending', '4']], params[:sortstatus]) %>
This will set the selected item to the current value of params[:sortstatus]
精彩评论