所以我在过去的一段时间里有过各种形式的这种工作,但从来没有一起工作。
作为参考,我有类别/品牌/产品,正确的关系有效:该网站是http://emeraldcityguitars.com以查看它的实际效果。
所以在我的品牌控制器中显示动作:
@category = Category.find_by_url_name(params[:category_id])
@brand = Brand.find(params[:id])
@search = Product.brand_id_equals(@brand.id).category_id_equals(@category.id).descend_by_price
@products = @search.paginate(:page => params[:page])
这很好用,正如我的日志中所证明的那样:
Category Load (25.6ms) SELECT * FROM "categories"
Category Load (0.2ms) SELECT * FROM "categories" WHERE ("categories"."url_name" = 'acoustic-guitars') LIMIT 1
Brand Load (0.6ms) SELECT * FROM "brands" WHERE ("brands"."id" = 14)
Product Load (4.8ms) SELECT * FROM "products" WHERE ((products.category_id = 3) AND (products.brand_id = 14)) ORDER BY products.price DESC LIMIT 6 OFFSET 0
SQL (0.2ms) SELECT count(*) AS count_all FROM "products" WHERE ((products.category_id = 3) AND (products.brand_id = 14))
Rendering template within layouts/application
Rendering brands/show
您可以看到它的抢手产品按价格下降。
在我的 Brand#show 中,我正在执行以下操作:
<%- form_for [@category, @brand], :html => {:method => 'get', :id => 'sort_form', :class => 'sort_form'} do -%>
<label>Sort by: </label> <%= select_tag :order, product_sort_options %>
<%= submit_tag 'Go' %>
<%- end -%>
目标是用户可以按几个不同的选项进行排序。
我的 products_helper 中也有这个:
def product_sort_options
options_for_select([
['', nil],
['Newest to Oldest', 'descend_by_date'],
['Oldest to Newest', 'ascend_by_date'],
['Price: Highest to Lowest', 'descend_by_price'],
['Price: Lowest to Highest', 'ascend_by_price'],
['Name', 'ascend_by_name']
])
end
我遇到的问题是,如果我单击下拉菜单并将价格从最低到最高重新加载,则在 url 末尾带有“?order=ascend_by_price”,但产品的顺序没有变化。
任何帮助表示赞赏。