Nick
Nick

Reputation: 3090

How to create this route?

I have the following route:

resources :articles, except: [:index] do
   collection do
      get 'author/:id/articles' => 'articles#index', as: 'index_articles'
   end
end

This results in:

GET    /api/v1/articles/:id/author/:id/articles(.:format)

How can I turn this into the following?:

GET    /api/v1/author/:id/articles(.:format)  

Upvotes: 2

Views: 49

Answers (2)

Babar Al-Amin
Babar Al-Amin

Reputation: 3984

Write this

get 'author/:id/articles' => 'articles#index', as: 'index_articles'

Outside the resources :articles block.

Upvotes: 0

Richard Peck
Richard Peck

Reputation: 76784

# config/routes.rb
resources :articles, except: [:index]
resources :authors, path: "author", only: [] do
   resources :articles, only: :index, on: :member #-> url.com/author/:id/articles
end

Upvotes: 1

Related Questions