Setting up REST(ful) Routing

Karol Gurba
Jv Coder
Published in
3 min readMay 23, 2017

REST(ful) routing is one of the most important parts of your rails app. It sets up the different routes for your app to take you to different pages. If you didn’t get to read my post about setting up a new rails app with postgresql, check it out HERE.

So to start setting up your routes you first need to go to your routes page, which can be found in the config folder.

Now that you’re in your routes page, you can start setting up your REST(ful). The first thing to know before you start is CRUD, which stands for create, read, update, destroy. This will be the way you’re gonna want to set up your routes.

So the first route you’re going to want to set up is the index page, which is your home page. To set this up we’re going to use the HTTP verb GET, then you want to write: get ‘/examples’ => ‘examples#index’ . The hash rocket aka (=>) is there to show which page the path is redirecting to. You’ll now want to set up a view page for that path, save this page to the views folder of your app folder.

The next step is to set up the new route, to do this just input get ‘/examples/new’ => ‘examples#new’ . This sets up a route to the new page, where you can create new data. The view page for this route should have a form to input the new data.

Next step is the create route, this will actually create your new object and send it over to the database. To set up this route you’ll need to write: post ‘/examples’ => ‘examples#create’ .

So now that we created some new data, were going to want to to see that data, this is where the show page comes in. To set up this route you’ll need to input get '/examples/:id’ => ‘examples#show’ .

Now what if we want to edit any of our data? The edit page is here to help out with that. This route is set up by entering: get '/examples/:id/edit’ => ‘examples#edit’ . Similar to the new page, you’ll want to make a form in the view page in order to let the user edit their data.

Next we’ll need to send the updated data to the database. We can do this by making a update route. The set up for this route is: patch '/examples/:id’ => ‘examples#update’ .

To delete any data they you have created, you’ll need to set up a destroy route. To do this you simply enter: delete ‘/examples/:id’ => ‘examples#destroy’ .

Congratulations! You have just set up REST(ful) routing in your rails app!

--

--