leemour
leemour

Reputation: 12003

Nested get routes

I want to have a nested get inside a resources block. So I have the following routes:
/businesses/page/:page
and
/businesses/sold/page/:page
But I can't figure how to make it work. Here are my routes:

concern :pageable do
  get 'page/:page', action: 'index', on: :collection
end

resources :businesses, only: [:index, :show] do
  concerns :pageable

  get 'sold', action: 'sold', on: :collection, as: 'sold' do
    get 'page/:page', action: 'sold'
  end
end

How do I make it work?
UPDATE: I want the above routes to work. With my current code above, I don't get /businesses/sold/page/:page. They are not listed in rake routes:

            GET    /businesses/page/:page(.:format)    businesses#index
sold_businesses GET  /businesses/sold(.:format)        businesses#sold
businesses  GET    /businesses(.:format)               businesses#index
business    GET    /businesses/:id(.:format)           businesses#show

I'm using page in routes for pagination. I want to use only 1 controller for all these routes - BusinessesController.

UPDATE2: I managed to get the routes I needed, but this code looks ugly:

get 'sold', action: 'sold', on: :collection, as: 'sold'
collection do
  scope 'sold' do
    get 'page/:page', action: 'sold'
  end
end

How to refactor it?

Upvotes: 1

Views: 167

Answers (2)

fylooi
fylooi

Reputation: 3870

Wanted to namespace for DRYing, but looks like this is more efficient.

resources :businesses do
  get 'page/:page', on: :collection, action: :index
  get 'sold/page/:page', on: :collection, action: :index
end

Upvotes: 1

ilan berci
ilan berci

Reputation: 3881

resources :businesses do
  concerns :pageable do
    collection do
      get: 'sold'
    end
  end
end

I am not sure what you are asking but I think you want to paginate through your sold.. right?

Upvotes: 0

Related Questions