1

I have to models User and Blog. Their respective urls are:

test.com/u/user_name   # User
test.com/blogs         # Blog

I'm trying to achieve that the blogs are nested to the user which created it. E.g. test.com/u/user_name/blogs and for an article test.com/u/user_name/blogs/article_name.

But the index for all blogs should respond to: test.com/blogs.

right now my routes are like this:

resources :users do
  resources :blogs, except: [:index]
end 

resources :blogs, only: [:index]

Which doesn't work.. What am i missing?

Mini John
  • 7,855
  • 9
  • 59
  • 108

1 Answers1

0

Routes

#config/routes.rb
resources :blogs, only: :index #-> domain.com/blogs -> blogs#index
resources :users, path: "u" do #-> domain.com/u/:id
   resources :blogs #-> domain.com/u/:user_id/blogs/:id
end

You'll want to look up about the path argument for your routes

As a rule of thumb, you'll want to include any "non conventional" paths at the bottom of the routes file. As the routes are determined from the top to bottom, if you're capturing obscure paths near the top of the file, you'll end up causing conflict with your other routes

Richard Peck
  • 76,116
  • 9
  • 93
  • 147