0

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

Desired functionality

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.

Noz
  • 6,216
  • 3
  • 47
  • 82
  • Where is it that the code is falling down? – Taryn East Jul 17 '12 at 23:46
  • The values selected for both of the select_tags aren't actually being passed along with the link_to, perhaps because those values are set during runtime and the link gets generated server-side? – Noz Jul 17 '12 at 23:57

1 Answers1

1

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]) %>
Taryn East
  • 27,486
  • 9
  • 86
  • 108
  • Hi I appended the suggested params[:sort], but no dice. I believe it's because this doesn't ever actually get set. – Noz Jul 17 '12 at 23:55
  • when you click "sort" - what params get sent to your controller? – Taryn East Jul 17 '12 at 23:55
  • Just my authentication token, controller name and action name. – Noz Jul 17 '12 at 23:58
  • Ah... You haven't actually got your select_tags inside of a "form_tag"... do that, and change the "link_to" to be a normal submit button, and you should be good to go. – Taryn East Jul 18 '12 at 00:05
  • Ah, maybe I was trying to accomplish something that was overly elegant. See I was looking at this website [Skyrim Nexusmods](http://skyrim.nexusmods.com/mods/searchresults/?cat=95) and noticed that they were doing pretty much exactly what I was trying to accomplish but they weren't using a form. Looking a little closer I see that they're using Javascript which is exactly what I didn't want. I'll settle for the form. – Noz Jul 18 '12 at 16:01
  • I've actually ran into a problem with the form submission, perhaps you can take a look at it: [Ajax not sending form params](http://stackoverflow.com/questions/11547669/rails-3-ajax-form-not-sending-params) – Noz Jul 18 '12 at 18:10