【发布时间】:2011-08-11 06:34:47
【问题描述】:
我正在关注 Rails 3.0.5 和 ruby 1.9.2p180 中的 Railscasts #228。
除了将类名从 Product 更改为 Player 之外,我已逐字逐句地复制代码。我也跳过了 Ryan 添加箭头来表示排序方向的最后一部分。我能够加载正确的索引页面并使用所需的参数(方向和排序)查看所有所需的 URL,但单击时实际上没有发生任何事情。 URL 正在更改,但页面未重新加载。
这是我的代码:
应用程序助手
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
玩家控制器
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 代码以及其中的链接?