2014-12-10 52 views
0

我正在为学校项目构建待办事项列表Web应用程序,并且试图让列的标题可点击,并且当您单击他们,他们整理信息。我已经在这里和谷歌教程尝试过很多解决方案,也许有人可以直接帮助我?如何在Ruby on Rails应用程序中对表的列进行排序

这里就是我想:
projects_controller.rb

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

    end 

projects.js

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

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> 

回答

1

好吧,让我引导你进行一些调试,因为它是为了学校。

第一个 在您的应用程序下有一个日志文件夹。开发日志会在您运行rails时填充。

看看您点击标题链接时会发生什么。 你想看到一个带有参数排序设置为标题的GET。

这是进入服务器的物理请求。 这可能是完全正确的,你上面有什么。

现在,我们要看看控制器,下一步是否正确。

因此,一个简单的方法来做到这一点,而不需要引入调试器就是使用raise。

控制器更改为这个 提高Project.order(PARAMS [:排序])。to_sql

这应该给你一个很好的错误消息再次,确认你正在做正确。

最后,我们会想看看视图

共享仅在表头,所以我做了如下假设,以什么代码的其余部分的样子。

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

我的直觉是你正在渲染@projects而不是@products,而@products是被排序的人。

最后,你的jQuery出现错误的...

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

或许应该

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