简体   繁体   English

如何在Ruby on Rails应用程序中对表的列进行排序

[英]How to sort columns of a table in a Ruby on Rails Application

I'm building a To Do List web application for a school project, and I'm trying to get the headers of the columns to be clickable and when you click them, they sort the information. 我正在为一个学校项目构建一个“待办事项列表” Web应用程序,并且试图使这些列的标题可单击,并且当您单击它们时,它们将对信息进行排序。 I've tried so many solutions on here and google tutorials, maybe someone can help me directly? 我在这里和Google教程上尝试了很多解决方案,也许有人可以直接帮助我?

Here's what I'm trying: 这是我正在尝试的:
projects_controller.rb projects_controller.rb

 def index
    @projects = Project.all
    @products = Project.order(params[:sort])

  end

projects.js projects.js

$(document).ready(function(){
    $('/index.html.erb').DataTable();
});

index.html.erb index.html.erb

<thead>
      <tr id="headers">
        <th><%= link_to "Title", :sort => "title" %></th>
        <th><%= link_to "Client", :sort => "client" %></th>
        <th>Description</th>
        <th>Hours</th>
        <th><%= link_to "Done", :sort => "done" %></th>
        <th colspan="3"></th>
      </tr>
</thead>

Ok, let me walk you through some debugging since it is for school. 好的,因为它是用于学校的,所以让我指导您进行一些调试。

First under your application there is a log folder. 首先在您的应用程序下有一个日志文件夹。 The development log gets populated when you run rails s. 运行rails时,将填充开发日志。

Look at what happens when you click the Title link. 查看单击“标题”链接时发生的情况。 You want to see a GET with params sort set to title. 您希望看到一个将参数排序设置为标题的GET。

That is the physical request that is coming into your server. 那是进入服务器的物理请求。 That is probably perfectly ok by what you have above. 通过上面的内容,这可能是完全可以的。

Now, we want to see if the controller, the next step is doing it right. 现在,我们要看看控制器是否正确,下一步是否正确。

So an easy way to do that without introducing debuggers is to use raise. 因此,在不引入调试器的情况下执行此操作的简单方法是使用抬高。

Change your controller to this raise Project.order(params[:sort]).to_sql 更改控制器以提高Project.order(params [:sort])。to_sql

This should give you a nice error message that again, confirms you are doing it correctly. 这应该会给您一个不错的错误消息,再次确认您做得正确。

Finally we are going to want to look at the view 最后我们要看一下视图

You are only sharing the head of the table so I am making the following assumption as to what the rest of the code looks like. 您只共享表的头部,因此我对其余代码的外观进行了以下假设。

<table id="someid">
    <thead>
        <tr id="headers">
            <th><%= link_to "Title", :sort => "title" %></th>
            <th><%= link_to "Client", :sort => "client" %></th>
            <th>Description</th>
            <th>Hours</th>
            <th><%= link_to "Done", :sort => "done" %></th>
            <th colspan="3"></th>
        </tr>
   </thead>
   <tbody>
        <% @products.each do |product| %>
        ... COLUMNS setout here ....
        <% end %>
   </tbody>
</table>

My gut inclination is that you are rendering @projects and not @products and @products is the one being sorted. 我的直觉是您正在渲染@projects,而不是@products,而@products是正在排序的人。

Finally, your jquery appears wrong... 最后,您的jquery出现错误...

$(document).ready(function(){
    $('/index.html.erb').DataTable();
});

Should probably be 应该是

$(document).ready(function(){
    $('#someid').DataTable(); //uses a selector such as the elements id or the class etc 
});

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

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