我遇到了一个问题,我已经打了几个小时,但是自己无法弄明白或在网上找到答案。
基本上,我有一个显示访问列表的页面,可以按日期和位置过滤访问次数。日期过滤器是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
有什么建议吗?
答案 0 :(得分:1)
假设idea_finder_path
映射到您在问题中分享的索引控制器操作,您似乎将options_for_select
的默认值设置为@locations_list
。
考虑构建这样的多选标签:
<%= select_tag "location_ids", options_from_collection_for_select(@locations_list, "id", "name", @selected_locations.collect{ |l| l.id }), :multiple => true %>
控制器的索引操作更新@selected_locations
实例变量以包含所选值。
请务必将"name"
替换为包含您在位置模型中关注的显示属性的列。