Railscasts #228 - 可排序的表列

发布于 2024-09-26 11:21:01 字数 303 浏览 8 评论 0原文

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

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

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


#view
<%= sortable "age" %>

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

I am following Ryan Bates' railscast on Sortable Table Columns and I have successfully gotten a column to sort ascending and descending.

My table is more complex than in the Railscast because I have columns from different tables.

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


#view
<%= sortable "age" %>

How do you add sortable columns for related tables such as manufacturers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2024-10-03 11:21:01

我有类似的问题。通过以下内容修复了它:

 <%= 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

I had a similar issue. Got it fixed with something like:

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

The sort function in the application_controller should be something like this:

 def sort_column
     ['manufacturers.name', 'cars.age'].include?(params[:sort]) ? params[:sort] : 'cars.age'
 end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文