Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Nikola Novakovic
Nikola Novakovic
4,171 Points

Ruby On Rails Routes

Hey there! :)

This is sorta the second part of the question I had posted a bit earlier , but not related to it, so I decided to make this a separate question. Also last question was pretty long and if asked this , it would get even longer and we do not want that :)

I figured out that routes can be generated through some generators that come with Ruby and they do a lot of work for us and then we just add additional routes if we need some. ( Correct me if I am wrong here please :) )

But I would like if someone could explain a bit more in depth how they actually work and what does that URI pattern actuall mean? I know that there is documentation , but it can really get quickly overwhelming reading that for a new guy in rails and maybe someone could really explain it in a simple way. So I am providing a screenshoot of how my routes currently look like for ODot and maybe someone can let me know what does all of those symbols doing in there and what is that :format standing for ? I see that there is a symbol :id in URI patterns and that is the current ID of an item from database but really can not figure out what the :format does :)

Here is a screenshoot: http://screencast.com/t/ibS4AXpji2r

Thanks in advance! :)

P.S. Geoff Parsons I think this will be a breeze for you haha! :)

2 Answers

The symbols in a route will end up being keys in your params hash. In a typical, non-nested, RESTful route you would only end up with params[:id]. For an example look at GET /todo_lists/:id.

The :format is for managing multiple response formats such as XML or JSON in your response and is optional. These will be handled for you based on the type of request. Should someone have requested /todo_lists/1.json then params[:id] would be 1 and the format would be JSON.

When defining routes with resources or via the rails generator you'll get the default CRUD routes. Which will typically only use :id, :format, and maybe a nested ID such as :todo_list_id. You can also define custom routes via strings which allow for custom bound parameters.

  get '/posts/:category/:id', to: 'posts#show'

I agree that it can be a bit daunting at first but the Rails Routing Guide really covers a lot of this stuff in detail if you are looking for any more details.

Nikola Novakovic
Nikola Novakovic
4,171 Points

Thank you so much man. I still need to dig into the "nested route" a little bit but documentation covers it all :)

Brandon Barrette
Brandon Barrette
20,485 Points

The format here you can ignore (I think). You can actually require that the :id be a specific format:

http://joshuawood.net/validating-url-in-ruby-on-rails-3/

The idea is that the :id is for the main model and then if you have nested resources, that's why you see :todo_list_id in some of your URLs.