1

我遇到了一个问题,我已经解决了几个小时,但自己无法弄清楚或在网上找到答案。

基本上,我有一个显示访问列表的页面,能够按日期和位置过滤访问。日期过滤器是 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

有什么建议么?

4

1 回答 1

1

假设idea_finder_path映射到您在问题中共享的索引控制器操作,您似乎将您的默认值设置options_for_select为 be @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"为包含您在 Location 模型中关心的显示属性的任何列。

于 2012-09-06T00:02:51.853 回答