Cannon Moyer
Cannon Moyer

Reputation: 3174

Ruby on Rails - Kaminari Defaulting To The Wrong URL

I've got several routes that point to a single controller action like this:

get '/elliptical-incline-motors', to: 'search#category_search'
get '/incline-ramp-wheels', to: 'search#category_search'
get '/elliptical-drive-belt', to: 'search#category_search'
get '/elliptical-batteries', to: 'search#category_search'

My query looks like this:

@products = Product.joins(category_model: {category_brand: :category}).where("categories.name = ? AND products.status = ?", category, "Viewable On Website & Backend").order(popularity: :desc).page(page).per(25)

If I am on the elliptical-batteries page (or any of the other pages that use the same controller action) and I select next page or a page number, the requested url is always the elliptical-incline-motors uri with my new page number. It looks like Kaminari is grabbing the first route because when I change this route in the route file the pagination get request changes accordingly. Is there a work around for this without chaning my current routing structure?

Upvotes: 0

Views: 273

Answers (2)

user3141095
user3141095

Reputation: 139

maybe not the best solution, but you could try the following, though it would have to be the last route specified:

get '*path' => redirect('/search/:type')

Upvotes: 1

Ronan Louarn
Ronan Louarn

Reputation: 472

I think only one route is necessary, but you should send params to dispatch queries in action. Like that:

get '/search/:type', to: 'search#category_search'

And in your controller:

case params[:type]
when  "elliptical-incline-motors"
   method 1
when "incline-ramp-wheels"
   method 2
etc....
end

I hope it will help you

Upvotes: 2

Related Questions