0
我爲我的網站創建了一個過濾系統,允許用戶選擇產品類別(如「書籍」)並呈現該類別中的所有結果。我使用的代碼是從Railscast #228 Sortable Table Columns我該如何重寫它以在'category'列中查找特定的字符串?非常感謝!!如何重寫我的排序功能以查找特定的字符串?
產品控制器
class ProductsController < ApplicationController
helper_method :sort_column, :sort_direction
def sort_column
Product.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
助手
def sortable(column, title = nil)
title ||= column.titleize
css_class = column == sort_column ? "current #{sort_direction}" : nil
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}, {:class => css_class}
end
HTML
<tr>
<th><%= sortable "name" %></th>
<th><%= sortable "price" %></th>
<th><%= sortable "released_at", "Released" %></th>
</tr>