DEV Community

Brittany
Brittany

Posted on

Day 31 - #100DaysofCode - Rails Routes 101

Understanding the rails routes is something I seem to be struggling with and after walking through a few labs and watching youtube videos, I think the concept is beginning to make sense.

A route in Rails is just a ruby method. A method that takes two arguments. The first argument is a string and the second is a hash.

For example, using the items example from my past post:

Rails.application.routes.draw do

get '/items', controller: 'items', action:'index'

end
Enter fullscreen mode Exit fullscreen mode

The above is just a method that says when the user goes to '/items' we want to go to the items controller and then we want to go to the index action within that items controller.

Another way you may see it written is like this:

Rails.application.routes.draw do

get('/items', {:controller => 'items', :action => 'index'})

end
Enter fullscreen mode Exit fullscreen mode

The above says the same thing, it is still taking two arguments, a string argument '/items' and a hash with a key-value pair {:controller => 'items', :action => 'index'}

But the conventional way of seeing it in Rails is like this:

Rails.application.routes.draw do

#get ({'/items' => 'items#index'})
get   '/items' => 'items#index'

end
Enter fullscreen mode Exit fullscreen mode

All three ways of writing the method will work.

But you will need more than one route right? Well, of course.

Another important route to know and understand is a dynamic route. For example, when you want to view an item, you most likely will want to go to items/1 for the first item in your db and items/10293 for the 10293 item in your db.

In order to create that route in Rails you need the following:

Rails.application.routes.draw do

get '/items/:id' => 'items#show'

end
Enter fullscreen mode Exit fullscreen mode

The the colon is essentially creating a variable named id. It can be named anything but is usually referred to as an id because you will fetch the id attribute within your database. After creating the route we need to create a show action within the items controller and a show view.

Within your show method you should have the following code:

def show
  item = item.find(params[:id])
end

Enter fullscreen mode Exit fullscreen mode

The above code along with the route allows us to find the information in the database associated with a particular id.

How does this happen?

Well, because of routes and rails.

To break it down the best that I can, when the user visits a site and goes to a dynamic route like items/1, they are sending data. The data in this case is a hash holding a key of 1 aka {"id" => 1}. That is automatically generated and sent with the params so that we are able to use it in our controller to search the db and give/show the user what they requested.

Rails is not magic but it is very impressive.

When checking what routes you have available to you try visiting www.localhost/3000/rails/info/routes when you are on your local computer or running rails routes in your terminal.

Also, another useful website for using rails restful routes is restular

Song of the day:

Top comments (0)