我有一個表格顯示一些不同的數據屬性,我遵循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
這裏是截圖的排序方法,這是結果點擊「羣組」標題。正如你所看到的,它會回覆到電話號碼。 *注 - 每個其他標題都會正確排序並顯示排序箭頭。
是的,我想按組名排序 – Bitwise
@ CameronBass如何在該列中有多個組時按組名稱排序?如果有一個帶有「訪客」的專欄,一個帶有「訪客,員工」和一個帶有「志願者,跳舞」的專欄 - 您喜歡的訂單是什麼,爲什麼? – spickermann
我只是按字母順序思考,就好像只有'員工'中有一個人,而'志願者'中有一個人,用戶可以按這種方式排序。誠然,它不會很有用,但它會保持整個頁面的連續性。 – Bitwise