我正在为学校项目构建待办事项列表Web应用程序,并且我正在尝试使列的标题可单击,当您单击它们时,它们会对信息进行排序。我在这里尝试了很多解决方案和谷歌教程,也许有人可以直接帮助我?
这是我正在尝试的:
的 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>
答案 0 :(得分:1)
好的,让我带你完成一些调试,因为它是为了学校。
首先 在您的应用程序下有一个日志文件夹。运行rails s时会填充开发日志。
查看单击“标题”链接时会发生什么。 你想看到一个GET,其中params排序设置为title。
这是进入您服务器的物理请求。 你可能完全没问题。
现在,我们想看看控制器,下一步是否正确。
因此,在不引入调试器的情况下,一种简单的方法是使用raise。
将您的控制器更改为此 提升Project.order(params [:sort])。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
});