我下面Railscasts#228在Rails的3.0.5和紅寶石1.9.2p180。的Rails 3:排序的列
我抄碼近逐字從逐字從產品改變類名球員除外。我也在跳過Ryan添加箭頭來表示排序方向的最後部分。我可以加載正確的索引頁面,並查看所有需要的網址和所需的參數(方向和排序),但實際上沒有發生點擊事件。 URL正在更改,但該頁面未重新加載。
這裏是我的代碼:
ApplicationHelper
def sortable(column, title = nil)
title ||= column.titleize
direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
link_to title, :sort => column, :direction => direction
end
PlayersController
def index
@players = Player.order(sort_column + ' ' + sort_direction)
end
private
def find_team
session[:team] ||= Team.new
end
def sort_column
Player.column_names.include?(params[:sort]) ? params[:sort] : "name_e"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
感謝您的幫助!
編輯:按照要求,HTML代碼。您可以看到Position的鏈接目前是desc,因爲我位於:http://localhost:3000/players?direction=asc&sort=abb_pos。但是,此列或任何其他列沒有發生實際排序。
<th><a href="/players?direction=asc&sort=name_e">Name</a></th>
<th><a href="/players?direction=asc&sort=team_e">Team</a></th>
<th><a href="/players?direction=desc&sort=abb_pos">Position</a></th>
<th><a href="/players?direction=asc&sort=height">Height</a></th>
<th><a href="/players?direction=asc&sort=weight">Weight</a></th>
你能提供一些運行時的html代碼它的鏈接?? – Lichtamberg