2012-10-08 16 views
0

我有兩個has_and_belongs_to_many關係項目和類別的表。link_to點擊事件排序表

我列出我所有的項目記錄在我的意見/項目/ new.html.erb這樣的:

<% @projects.each do |project| %> 
    <tr class="project <% project.categories.all.each do |cat| %><%= cat.name %> <% end %>"> 
    <td><a href='project/<%= project.id %>'><%= project.filename %></a></td> 
<td><%= project.author %> 
<td><% project.categories.all.each do |cat| %><%= cat.name %>, <% end %></td> 
    </tr> 
<% end %> 

並已定義的link_to幾個遠程鏈接就像這樣:

<div class="filterBy">SORT BY:</div> 
<div class="filterBy"><%= link_to "Category", :update => "projects", :url => { :action => "sortTable", :filter => "Category" }, :remote => true %></div>           
<div class="filterBy"><%= link_to "Author", :update => "projects", :url => { :action => "sortTable", :filter => "Author" }, :remote => true %></div> 
<div class="filterBy"><%= link_to "Date", :update => "projects", :url => { :action => "sortTable", :filter => "Date" }, :remote => true %></div> 

而且在我的projects_controller我有

def sortTable 
    @projects = Project.find(:all, :order => params[:filter]) 
end 

當我點擊鏈接,但沒有任何反應。我究竟做錯了什麼?我希望它在不刷新頁面的情況下進行更新。如果需要的話,我的routes.rb看起來像

Docside::Application.routes.draw do 
    resources :projects 
    resources :categories 
    #get "home/index" 
    root :to => "projects#new" 
    match 'project/new',:controller=>"projects",:action=>"create" 
    match "project/:id", :controller => "projects", :action=>"download" 
end 

回答

0

在我們的代碼中,u [R發送一個遠程服務器請求,只是爲了獲得排序的數據。這將會使用多個請求來終止服務器。由於你的列表頁面的主要要求是排序數據而不刷新頁面,你可以使用它的數據表。樣本頁面上,使用數據表 -

添加到UR appliation.js

//= require jquery.dataTables.min 

在用戶控制器

def index 
    @users = User.all #collect all the users 
end 
在烏拉圭回合列表/索引視圖

%table#users 
    %thead 
    %tr 
     %th User Name 
     %th User Age 
    %tbody 
    %tfoot 

:javascript 
    var users_list = true; 
    var users = #{@users.to_json}; #convert the object collection to json 
在烏爾users.js

.coffee文件

if users_list? 
    usersColumnDefs = [ { "bSearchable": true, "bSortable": true, "bVisible": true, "aTargets": [ 0, 1 ] }, { "sClass": "user-name", "aTargets": [ 0 ] }, { "sClass": "user-age", "aTargets": [ 1 ] } ] 

    data = $.map(users, (user) -> 
    return [[ user.name, user.age ]] 
) 

    $('#users').dataTable 
    "bSort": false, 
    "bPaginate": false, 
    "sDom": sDom 
    "aaData": data 
    "aoColumnDefs": usersColumnDefs 

另請參閱 What is the advantage of using jquery DataTables for large databases?