简体   繁体   English

排序时不删除URL中的参数[RAILS 3]

[英]Not delete params in URL, when sorting [ RAILS 3 ]

I have sortable table columns, made like that http://asciicasts.com/episodes/228-sortable-table-columns And I have simply filter options for two columns in table, made at select_tag (GET method). 我有可排序的表列,其格式如下: http ://asciicasts.com/episodes/228-sortable-table-columns并且我只对表中两列的过滤器选项进行了过滤,这些列是在select_tag(GET方法)上进行的。

This two function don't work together. 这两个功能不能同时使用。 When I change filter, the sort parameter disappear and inversely. 当我更改过滤器时,排序参数反而消失了。

<th><%= sortable "Id" %></th>
<th>
  Status<br/>
  <form method="get">
  <%= select_tag(:status, options_for_select([['All', 'all']]+@statuses, params[:status]),{:onchange => 'this.form.submit()'}) %> 
</th>
<th><%= sortable "Operation" %></th>
<th>
  Processor<br/>
  <%= select_tag(:processor, options_for_select([['All', 'all']]+@processor_names, params[:processor]),{:onchange => 'this.form.submit()'}) %>
  </form> 
</th>

The answer from Kamil works fine for a single controller. Kamil的答案对于单个控制器来说效果很好。 Episode 228 puts sortable method inside application_helper so if you want use it in other controllers you must add all the params. 第228集将可排序方法放在application_helper中,因此,如果要在其他控制器中使用它,则必须添加所有参数。 You can solve with params.merge 你可以用params.merge解决

link_to title, params.merge(:sort => column, :direction => direction), {:class => css_class}

hehe, trivial solution 呵呵,平凡的解决方案

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, {:status => params[:status], :processor => params[:processor], :sort => column, :direction => direction}, {:class => css_class}  

end 结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM