Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3.2.7 : Multiselect (select_tag) not staying selected
    text
    copied!<p>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.</p> <p>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).</p> <p>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.</p> <p>Here are the relevant files:</p> <p>index.html.erb</p> <pre><code>&lt;%= javascript_include_tag "jquery.multiselect.min" %&gt; &lt;%= stylesheet_link_tag "jquery.multiselect.css" %&gt; &lt;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&gt; &lt;h1&gt;Read-Aloud Ideas&lt;/h1&gt; &lt;%= link_to 'View list of themes', themes_path %&gt; &lt;h3&gt;Filter Results&lt;/h3&gt; &lt;%= form_tag(idea_finder_path, :method =&gt; "get", :id =&gt; "idea_finder") do %&gt; &lt;%= hidden_field_tag :direction, params[:direction] %&gt; &lt;%= hidden_field_tag :sort, params[:sort] %&gt; &lt;%= label_tag :min_date, 'From' %&gt; &lt;%= text_field_tag :min_date, params[:min_date], :id =&gt; "min_date" %&gt; &lt;%= label_tag :max_date, 'To' %&gt; &lt;%= text_field_tag :max_date, params[:max_date], :id =&gt; "max_date" %&gt; &lt;br/&gt; &lt;%= label_tag :location_ids, 'Locations' %&gt; &lt;%= select_tag :location_ids, options_for_select(@locations_list.collect {|col| [col.name, col.id]}, @locations_list),:multiple =&gt; true, :id =&gt; "locations" %&gt; &lt;%= submit_tag "Filter" %&gt; &lt;% end %&gt; &lt;div id="idea_finder_results"&gt; &lt;%= render 'results' %&gt; &lt;/div&gt; </code></pre> <p>_results.html.erb</p> <pre><code>&lt;%= paginate @visits %&gt; &lt;table class="pretty"&gt; &lt;tr&gt; &lt;th&gt;&lt;%= sortable "date", "Date" %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= sortable "location_id", "Location" %&gt;&lt;/th&gt; &lt;th&gt;&lt;%= sortable "theme_id", "Theme" %&gt;&lt;/th&gt; &lt;/tr&gt; &lt;% for visit in @visits %&gt; &lt;% if !@locations.nil? &amp;&amp; (@locations.include? visit.location) &amp;&amp; visit.theme.isrecommended %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= visit.date %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= visit.location.name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to visit.theme.name, theme_path(visit.theme.id) %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/table&gt; &lt;%= paginate @visits %&gt; </code></pre> <p>application_helper.rb</p> <pre><code>module ApplicationHelper def sortable(column, title = nil) title ||= column.titleize css_class = column == sort_column ? "current #{sort_direction}" : nil direction = column == sort_column &amp;&amp; sort_direction == "asc" ? "desc" : "asc" link_to title, params.merge(:sort =&gt; column, :direction =&gt; direction, :page =&gt; nil), {:class =&gt; css_class} end end </code></pre> <p>controller</p> <pre><code> 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 </code></pre> <p>Any suggestions?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload