0

我正在关注 Ryan Bates 在 Sortable Table Columns 上的 railscast,并且我已经成功地获得了一个用于升序和降序排序的列。

我的表比 Railscast 中的表更复杂,因为我有来自不同表的列。

# controller
@cars = Car.find(:all).order(sort_column + " " + sort_direction).includes(:manufacturers)


#view
<%= sortable "age" %>

如何为制造商等相关表添加可排序列?

4

1 回答 1

3

我有一个类似的问题。用类似的东西修复它:

 <%= sortable "manufacturers.name", "Manufacturer name" %>
 <%= sortable "cars.age", "Age" %>

application_controller 中的排序函数应该是这样的:

 def sort_column
     ['manufacturers.name', 'cars.age'].include?(params[:sort]) ? params[:sort] : 'cars.age'
 end
于 2010-10-10T08:27:13.847 回答