我下面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