Kobius
Kobius

Reputation: 714

Ruby on Rails - Routes - taking a step out of the URL

My routes.rb file:

resources :forums do
   resources :discussions, 
end

I have my project all setup and working, the URL for a forum topic is:

localhost:3000/forums/ - (forum name using permalink) - /topics/1

I would like to replace this with a slightly easier version:

localhost:3000/forums/ - (forum name using permalink) - /1

Basically, removing the 'topics' portion from the URL and disabling '/topics' completely - so that it can't be accessed.

I have setup the show page of a forum to display a list of its topics. What's the best way to go about achieving this? Many thanks in advance!

Upvotes: 0

Views: 94

Answers (1)

klump
klump

Reputation: 3269

You need to add a route to your config/routes.rb.

In my eyes the easiest would be to define a default route for /forums/general-chat.

match 'forums/:forumname/general-chat/:id => 'controller#method'

To disable the old route with topics in:

match 'forums/:forumname/topics/:id' => redirect("forums/%{forumname}/%{id}")

The rails routing guide is great!

Upvotes: 1

Related Questions