2014-12-10 50 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 
}); 
相關問題