I was just looking at Ryan Bates railscast tutorial on Sortable Table Columns which with a few modifications becomes ajaxified in one of his later videos(Search, Sort, Paginate with AJAX). I think his solution worked great, it's clean and it degrades easily.
One problem though was the fact that he was using tables for his sorting functionality, whereas I want to use select inputs instead.
Currently I have it setup with two select boxes, one for the column and another for the search direction, as well as a link to initiate the ajax request (see below for screenshot).
However Ryan Bates had it setup so he had an individual link for each sortable column, whereas I'm only going to have one link. I was wondering if anybody knew of a way to extrapolate on what he had and merge the params with the values on a select box.
This is what I had thus far...
<table>
<tr>
<th>Sort by</th>
<th>Order</th>
</tr>
<tr>
<td>
<%= select_tag "sort", options_for_select([["Creation Date", "created_at"], ["File Name", "name"], ["Rating", "rating_average"], ["Downloads", "downloads"]]) %>
</td>
<td>
<%= select_tag "direction", options_for_select([["Descending", "desc"], ["Ascending", "asc"]]) %>
</td>
<td>
<%= link_to "Sort", params.merge(:sort => params[:sort], :direction => params[:direction], :page => nil), :class => "icon" %>
</td>
</tr>
</table>
If possible I don't want to have to restrict the functionality to only users with JavaScript enabled.
AFAICS, params from select-tags will come through in pretty much the same way as any other param - so merging them should work the same way. The only difference is that you need to pass params[:sort]
into the select-tag. Have a look at how you pass in the currently-selected-value here:
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag
and
http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/options_for_select
Which suggests a format:
<%= select_tag "sort", options_for_select([["Creation Date", "created_at"], ["File Name", "name"], ["Rating", "rating_average"], ["Downloads", "downloads"]], params[:sort]) %>