How would you set up a Sort dropdown?

I can set up the dropdown with links based on bootstrap code but what is the best way to link to sorted listings. Say i want price from low to high..and i want to put this dropdown on multiple pages - index, category, seller page, and search results page. What would the link_to look like? I read that searchkick has some sort capabilities too but not sure if I can use it for my purpose.

Posted about 5 years ago by Ashfaaq Moosa
Posted about 5 years ago by Alex Yang

Here's an example. If you're link_to is:
<%= link_to "Highest priced products", listings_path(sort_order: "high_to_low") %>

then in your listings_controller.rb file, under the index action (because that's the action that corresponds to listings_path), you'll need to check for that sort_order param, like this:

def index
if params[:sort_order] == "high_to_low"
@listings = Listing.all.order("price DESC")
end
end

You can also see this approach applied in the 'Add Categories - Part III' video in the Features Library: https://baserails.com/videos/add-categories-part-iii.

0
Posted about 5 years ago by Ashfaaq Moosa

To clarify, i can set up the dropdown but I need help with the functionality and what to link_to. I want to sort by created date and price both ways.

0