weixin_33726318 2012-07-17 23:42 采纳率: 0%
浏览 17

Rails与select合并参数

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.

  • 写回答

1条回答 默认 最新

  • lrony* 2012-07-17 23:48
    关注

    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]) %>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算