I would like my website to allow users to select a product category such as ‘books’ and return the results that are in that category. The code I’m using is from Railscast #228 Sortable Table Columns.
How do I change the code to look for a specific string in the category column?
Controller:
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
Helper function:
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>
Figured it out, it was actually pretty easy, created a new function and instead of using strings (which was not working) I used id #s and then plugged in ‘sort_category’ into my Solr function for search.