开发者

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).

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开发者_开发百科>


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


The answer from Kamil works fine for a single controller. Episode 228 puts sortable method inside application_helper so if you want use it in other controllers you must add all the params. You can solve with params.merge

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜