RESTful Routes
Tags: ruby, codenewbie, womenintech, html
Published: 2020-09-27 22:34:06

Hi there, I will be writing about RESTFUL ROUTES  by explaining each of the verbs and actions within my sinatra web app.

Restful routes are a design pattern that allows easy data manipulation, as developers and users can recognize the code and understand it better. 
Restful routes provides mapping between HTTP verbs, such as GET, POST, PUT, PATCH and DELETE to controller actions (Create, Read, Update, Delete) very well known as CRUD.
Instead of depending on URL to indicate where you are in your browser, Restful routes uses HTTP REQUESTS to send and receive actions from the USER. These requests work with my application on receiving a request, examining it, as well as identifying the HTTP method and URL in my controller and executing the code with the action to give a response to the USER.

## PUT-PATCH-DELETE ##

These are hidden requests sent by POST request to the browser, as HTML is stateless which means it supports GET and POST requests only. To perform the three actions above, we use Rack::MethodOverride into your Config.ru file as "use Rack::MethodOverride" middleware. We have to use a post method in the html form to simulate a request with a non supported method in order to make this work, you will have to type  ```<input type="hidden">``` Following an ```<input type="hidden" name="_method">``` The method's name has to be included as the value.

```
<form method="post" action="/logout">
  <input  type="hidden" name="_method" value="DELETE">
  <input type="submit" value="Log out">
</form>
```

I will explain each action I've created on my web app one by one.

 INDEX ACTION
 ```
get '/pets' do 
        authenticate 
        @pets = current_user.pets.all
        erb :'/pets/dashboard'
    end
 ```

This responds to a GET request to the route '/pets' and is the Index Action; it allows the view access to all the User's pets in the database through the instance variable @pets and render it to my erb :'/pets/dashboard'

 NEW ACTION
 ``` 
get '/pets/new' do 
      erb :'/pets/new'
    end 
```

This is a GET request to load the form to create a new pet.

3. CREATE ACTION 
 ```
post '/pets' do
      @pet = Pet.new(name: params[:name],age: 
          params[:age],notes: params[:notes])
      if @pet.save
        current_user.pets << @pet
        redirect "/pets/#{@pet.id}"
      else  
        erb :'/pets/new'
      end 
```

This responds to a POST request and creates a new pet based on the params and saves it to the database. Once the pet is created this action redirects to show.erb.

 SHOW ACTION 
  ```
 get '/pets/:id' do
      @pet = Pet.find_by(id: params[:id])
      erb :'/pets/show'
    end 
```

The controller action responds to a GET request to the path '/pets/:id' as this route is a dynamic one it will access the pet by its ID in the view through the params hash.

EDIT ACTION 
```
get '/pets/:id/edit' do
        @pet = Pet.find_by(id: params[:id])
        dont_edit 
        erb :'/pets/edit'
    end
 ```

This route loads the edit form in the browser by making a GET request.

```
patch '/pets/:id' do 
        @pet = Pet.find_by(id: params[:id])
       dont_edit
        @pet.update(name: params[:name],age: params[:age],notes: params[:notes])
        @pet.save
        redirect '/pets'
    end
```

After submitting the form, the PATCH requests to the path '/pets/:id', first it will pull the pet by name, age and notes through params, then saves the pet and redirects to the dashboard '/pets'

DELETE ACTION 

``` 
delete '/pets/:id' do 
        @pet = Pet.find_by(id: params[:id])
        dont_edit
        @pet.destroy
        redirect '/pets'
    end 
```
This form is submitted via a DELETE request to the path '/pets/:id', this action finds the pet by the ID in the database, deletes it and redirects to '/pets'

Thanks for reading!