Sorting Products on Taxon#show

1,313 views
Skip to first unread message

greeneggs

unread,
Oct 28, 2010, 6:10:16 PM10/28/10
to Spree
I'm a little lost on how to sort products (sort by name, price, etc)
on the Taxon#show. I read the documentation on named scopes, but I
still don't quite understand how to proceed. Any pointers?

greeneggs

unread,
Oct 29, 2010, 12:09:35 PM10/29/10
to Spree
Can anybody give me advice?

nikimere

unread,
Oct 29, 2010, 12:23:27 PM10/29/10
to Spree
Do you mean the default order or user selected reordering? i.e.
buttons that say "Order by price, Order by name" etc...

- Niki

greeneggs

unread,
Oct 29, 2010, 12:28:29 PM10/29/10
to Spree
More particularly user selected reordering. I am curious about
setting the default ordering too. Thanks for responding.

-Scott

nikimere

unread,
Oct 29, 2010, 12:38:03 PM10/29/10
to Spree
I'm still trying to figure out the default myself. Haven't spent too
much time on it though.
For user clickable reordering it's fairly straightforward. See below:

<%= link_to "Price - Down", seo_url(@taxon)+"s/
ascend_by_master_price" %>
<%= link_to "Price - Up", seo_url(@taxon)+"s/descend_by_master_price"
%>
<%= link_to "Name - Down", seo_url(@taxon)+"s/ascend_by_name" %>
<%= link_to "Name - Up", seo_url(@taxon)+"s/descend_by_name" %>

That is code that would go on your taxon/show page.
Basically all you have to do is append "/s/" onto the end of your URL
then the name of the scope, i.e. ascend_by_master_price etc...

I've done it for a client, it's a bit of an ugly implementation at the
moment but you can see it here: http://www.murraymotorsport.ie/t/products/racewear/fireproof-suits
It's on the top right above the products.

Hope that puts you in the right direction.

- Niki

greeneggs

unread,
Nov 1, 2010, 3:54:03 PM11/1/10
to Spree
Thanks Niki. Yeah, that helps, but I was really hoping to use a drop
down. Using links works, but I don't really like creating new urls
for a simple sorting. In my experience, it's bad practice to have new
urls for the same content because google / search bots will spread
your "juice" to each sort option.
Any how, this is what I attempted to do:

Added a hash to the Taxons Controller via class_eval
@sort_options = {
"Most Popular" => "descend_by_popularity",
"Name: A to Z" => "ascend_by_name",
"Name: Z to A" => "descend_by_name",
"Price: High to Low" => "descend_by_master_price",
"Price: Low to High" => "ascend_by_master_price",
}

Then I created the following drop down that renders on Taxons#show

<% form_for :taxon, :url => seo_url(@taxon) do |f| %>
<%= select_tag 'product_group_query',
options_for_select(@sort_options.each_pair { |key, value|
[key,value] }), :onchange => "this.form.submit();" %>
<% end %>

Here's the interesting part, the "ascend_by_WHATEVER" works, but the
the "descend_by_WHATEVER" gives me the following Exception:
NoMethodError (undefined method `pop' for
"descend_by_popularity":String):
spree (0.11.0) app/models/product_group.rb:76:in `from_route'
spree (0.11.0) lib/spree/search.rb:23:in `retrieve_products'
spree (0.11.0) app/controllers/taxons_controller.rb:13:in
`load_data'

I wonder if I'm doing something wrong or maybe something got screwed
up via implementation of the site. Maybe I should abandon my idea,
but it seems like you should be able to post to the taxons controller
witha a "product_group_query" parameter and have it sort.

Has anyone else tried this? Am I nuts for attempting this?

-Scott

greeneggs

unread,
Nov 2, 2010, 10:51:52 AM11/2/10
to Spree
Bump...anybody?

Victor Ilyukevich

unread,
Nov 2, 2010, 4:18:59 PM11/2/10
to spree...@googlegroups.com
I think, you should rewrite method object for taxon model and setup default sorting there.
And also you can submit your form for the same url with GET method and in your new method object you will check if there params[:sord_by] and if it needed - sort results.

Bump...anybody?
--
You received this message because you are subscribed to the Google Groups "Spree" group.
To post to this group, send email to spree...@googlegroups.com.
To unsubscribe from this group, send email to spree-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spree-user?hl=en.




--
Виктор Ильюкевич
cell: +375 29 77 333 50
www: http://bestblog.name
skype: bd.studio

Sonu Singh

unread,
Apr 1, 2016, 4:17:50 AM4/1/16
to Spree
Hi Niki,

I am facing the same issue and applied your approach getting the same result. I am using code like this
in taxons#show
@sort_options = { 
          "Most Popular" => "descend_by_popularity", 
          "Name: A to Z" => "ascend_by_name", 
          "Name: Z to A" => "descend_by_name", 
          "Price: High to Low" => "descend_by_master_price", 
          "Price: Low to High" => "ascend_by_master_price", 
      }
show.html.erb
<%= form_for :taxon, :url => seo_url(@taxon), :method=> 'get' do |f| %> 
                    <%= select_tag 'product_group_query', 
                    options_for_select(@sort_options.each_pair {  |key, value| 
                    [key,value]  }), :onchange => "this.form.submit();" %> 
                  <% end %> 
Could you please give me some idea why same result or I have to do any more.
Thank you so much in advance.

Sonu
Reply all
Reply to author
Forward
0 new messages