I’m having an issue I’ve been fighting for a few hours, and have not been able to figure it out myself or find an answer online.
Basically, I have a page that displays a list of Visits, with the ability to filter Visits by date and Location. The date filter are text_fields (min_date and max_date), while the location filter is a multiselect (using select_tag options_for_select).
The problem is that when I load the page after filtering, the multiselect does not stay persistent. The date fields will stay filled out after loading the page, but the location select will always revert back to having nothing selected. This happens if I refresh the page, switch the sorting column/order, or click the “Filter” button. The weird thing is that the location_ids parameter will be in the URL, but it just won’t be reflected in the form.
Here are the relevant files:
index.html.erb
<%= javascript_include_tag "jquery.multiselect.min" %>
<%= stylesheet_link_tag "jquery.multiselect.css" %>
<LINK REL=StyleSheet HREF="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.8/themes/ui-lightness/jquery-ui.css" TYPE="text/css" MEDIA=screen>
<h1>Read-Aloud Ideas</h1>
<%= link_to 'View list of themes', themes_path %>
<h3>Filter Results</h3>
<%= form_tag(idea_finder_path, :method => "get", :id => "idea_finder") do %>
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<%= label_tag :min_date, 'From' %>
<%= text_field_tag :min_date, params[:min_date], :id => "min_date" %>
<%= label_tag :max_date, 'To' %>
<%= text_field_tag :max_date, params[:max_date], :id => "max_date" %> <br/>
<%= label_tag :location_ids, 'Locations' %>
<%= select_tag :location_ids, options_for_select(@locations_list.collect {|col| [col.name, col.id]}, @locations_list),:multiple => true, :id => "locations" %>
<%= submit_tag "Filter" %>
<% end %>
<div id="idea_finder_results">
<%= render 'results' %>
</div>
_results.html.erb
<%= paginate @visits %>
<table class="pretty">
<tr>
<th><%= sortable "date", "Date" %></th>
<th><%= sortable "location_id", "Location" %></th>
<th><%= sortable "theme_id", "Theme" %></th>
</tr>
<% for visit in @visits %>
<% if !@locations.nil? && (@locations.include? visit.location) && visit.theme.isrecommended %>
<tr>
<td><%= visit.date %></td>
<td><%= visit.location.name %></td>
<td><%= link_to visit.theme.name, theme_path(visit.theme.id) %></td>
</tr>
<% end %>
<% end %>
</table>
<%= paginate @visits %>
application_helper.rb
module ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction, :page => nil), {:class => css_class}
end
end
controller
helper_method :sort_column, :sort_direction
def index
@locations_list = Location.all
@locations = Location.idea_search(params[:location_ids])
@visits = Visit.idea_search(params[:min_date], params[:max_date]).page(params[:page]).per(20).order(sort_column + " " + sort_direction)
end
def sort_column
if (Visit.column_names.include?(params[:sort]))
params[:sort]
else
"date"
end
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
Any suggestions?
Assuming that
idea_finder_pathmaps to the index controller action you shared in your question, it would seem you’re setting the default values of youroptions_for_selectto be@locations_list.Consider building your multi-select tag like this:
Where your index action of your controller updates the
@selected_locationsinstance variable to contain the selected values.Be sure to replace
"name"with whatever column holds the display attribute you care about in your Location model.