数時間戦ってきた問題があり、自分でそれを理解したり、オンラインで答えを見つけたりすることができませんでした。
基本的に、訪問のリストを表示するページがあり、日付と場所で訪問をフィルタリングする機能があります。日付フィルターはtext_fields(min_dateおよびmax_date)ですが、場所フィルターは複数選択です(select_tag options_for_selectを使用)。
問題は、フィルタリング後にページをロードすると、複数選択が永続化されないことです。ページを読み込んだ後も日付フィールドは入力されたままになりますが、場所の選択は常に何も選択されていない状態に戻ります。これは、ページを更新するか、並べ替えの列/順序を切り替えるか、[フィルター]ボタンをクリックすると発生します。奇妙なことに、location_idsパラメーターはURLに含まれますが、フォームには反映されません。
関連するファイルは次のとおりです。
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
コントローラ
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
助言がありますか?