我创建了一个简单的表排序器:
ProductsController:
helper_method :sort_column, :sort_direction
# GET /products
# GET /products.xml
def index
@products = Product.order(sort_column+ " "+sort_direction)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @products }
end
endApplication_helper
module ApplicationHelper
def sortable(column, title = nil )
title ||= column.titleize
direction = column == sort_column && sort_direction[:direction] == "asc" ? "desc" : "asc"
link_to title, {:sort => column, :direction => direction}
end
end和视图:
<tr>
<th><%= sortable "name"%></th>
<th><%= sortable "price"%></th>
<th><%= sortable "released" %></th>
</tr>在我尝试打开/products页面后,我得到了这个错误(Rails v3.0.3):
can't convert Symbol into Integer
Extracted source (around line #5):
2:
3: <table>
4: <tr>
5: <th><%= sortable "name"%></th>
6: <th><%= sortable "price"%></th>
7: <th><%= sortable "released" %></th>
8: </tr>和排序方法:
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发布于 2011-02-25 23:50:48
direction = column == sort_column && sort_direction[:direction] == "asc" ? "desc" : "asc"也许应该改成这样。sort_direction看起来像是一个字符串,但您将其视为散列。
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"https://stackoverflow.com/questions/5119192
复制相似问题