2016-04-03 106 views
0

我有一個表格顯示一些不同的數據屬性,我遵循railscast#228使列可排序。幾乎所有的工作除了一個名爲「groups」的標題,當我點擊鏈接我的方向箭頭中斷,它回覆到原始標題(爲了清楚起見,我會發布截圖)。我有一個助手,一個控制器和一個呈現問題代碼功能的視圖,現在我將顯示相應的代碼,並盡我所能來描述爲了幫助我而需要知道的一切。這是我的觀點。您可以在@people塊中看到<td><%= person.groups.order(:id).pluck(:name).to_sentence %></td>是問題所在。Glyphicon箭頭幫助排序列

<table class="table table-striped"> 
<thead> 
    <tr> 
    <th><%= sortable 'phone_number', 'Phone Number'%></th> 
    <th><%= sortable 'subscribed', 'Subscribed'%></th> 
    <th><%= sortable 'city' %></th> 
    <th><%= sortable 'state' %></th> 
    <th><%= sortable 'zip' %></th> 
    <th><%= sortable 'country' %></th> 
    <th><%= sortable 'groups' %></th> 
    <th><%= sortable 'created_at', "Joined" %></th> 
    </tr> 
    </thead> 

<% @people.each do |person| %> 
     <tr> 
     <td><%= person.phone_number %></td> 
     <td><%= person.subscribed? ? "Yes" : "No" %></td> 
     <td><%= person.city %></td> 
     <td><%= person.state %></td> 
     <td><%= person.zip %></td> 
     <td><%= person.country %></td> 
     <td><%= person.groups.order(:id).pluck(:name).to_sentence %></td> 
     <td><%= time_ago_in_words person.created_at %> ago</td> 
     </tr> 
     <% end %> 

這裏是排序的輔助方法

def sortable(column, title = nil) 
title ||= column.titleize 
if column != sort_column 
    css_class = nil 
    direction = "asc" 
elsif sort_direction == "asc" 
    css_class = "glyphicon glyphicon-triangle-top" 
    direction = "desc" 
else 
    css_class = "glyphicon glyphicon-triangle-bottom" 
    direction = "asc" 
end 
link_to "#{title} <span class='#{css_class}'></span>".html_safe, sort: column, direction: direction 
end 

這裏是我的控制器

def index 
@people = Person.order(sort_column => sort_direction) 
@groups = Group.all 
end 

這些都是私人

def sort_column 
    Person.column_names.include?(params[:sort]) ? params[:sort] : "phone_number" 
    end 

    def sort_direction 
%w(asc desc).include?(params[:direction]) ? params[:direction] : "asc" 
    end 

這裏是截圖的排序方法,這是結果點擊「羣組」標題。正如你所看到的,它會回覆到電話號碼。 *注 - 每個其他標題都會正確排序並顯示排序箭頭。

enter image description here

回答

2

你的問題的根源是不是在你在你的問題中提供的代碼,但在截屏的代碼示例。有這種方法,我以爲你實現了一個類似的方法:

def sort_column 
    Product.column_names.include?(params[:sort]) ? params[:sort] : "name" 
end 

注意groups在你的表是不是數據庫列的名稱,而是關聯到另一個模型的名稱。因此,此方法始終會返回"name"(或者我假設您的方法中爲"phone_number")。

您不能以與列相同的方式對活動記錄中的關聯進行排序。可能有解決方法,但取決於您對排序的定義groups關聯表示您是否想按組名稱,組數或組ID列出組列表中的條目?


如果你想實現通過關聯進行排序,你需要做兩件事情。首先讓sort_column方法除了列名接受團體名稱:

def sort_column 
    sortables = Person.column_names + ['groups'] 
    sortables.include?(params[:sort]) ? params[:sort] : 'phone_number' 
end 

而當你有地方處理協會排序的特殊情況下,第二個步驟,或許在模型中的scope

# in the model: 
scope :sort_by, lambda { |options| 
    if options.key == ['groups'] 
    scope = select('DISTINCT people.*').joins(:groups) 
    if options.values == ['asc'] 
     scope.order('MIN(groups.name)') 
    else 
     scope.order('MAX(groups.name)') 
    end 
    else 
    order(options) 
    end 
} 

# in the controller 
@people = Person.sort_by(sort_column => sort_direction) 

聲明:我不確定範圍實際上是否工作正常,就像您期望排序工作一樣。也許它會馬上提出一個錯誤。只是很難編寫這樣的代碼,而沒有實際運行數據庫的可能性。但你有這樣的想法:當你想通過關聯而不是列來排序時,它會變得混亂,因爲這與你看過的截屏無關......

+0

是的,我想按組名排序 – Bitwise

+0

@ CameronBass如何在該列中有多個組時按組名稱排序?如果有一個帶有「訪客」的專欄,一個帶有「訪客,員工」和一個帶有「志願者,跳舞」的專欄 - 您喜歡的訂單是什麼,爲什麼? – spickermann

+0

我只是按字母順序思考,就好像只有'員工'中有一個人,而'志願者'中有一個人,用戶可以按這種方式排序。誠然,它不會很有用,但它會保持整個頁面的連續性。 – Bitwise