loading...

Day 11 : #100DaysofCode - RESTful Routes

sincerelybrittany profile image Brittany Updated on ・4 min read

#100DaysofCode (65 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 63 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables

Song of the day

The Internet

HTTP

HTTP stands for The Hypertext Transfer Protocol and it helps clients (aka browsers such as Chrome/Safari/Internet explorer) to communicate with a server. A browser sends an HTTP request to a server and then the server provides a response/status back.

HTTP Verbs

There are most common HTTP verbs/request you will see are:

GET This request is used to GET information from the server using the url inputted. Request using GET can only retrieve data.
POST This request is used to send (post) data to the server, usually by submitting a form.
PUT This request uses the url to supply a modified version of the requested resource which replaces the original version of the resource (it is used to update information by replacing the original content)
PATCH This request supplies a set of instructions to modify the resource. It is similar to a PUTS request in that it updates the information and should only be used if the patch document is smaller than the size of the new version of the source sent.
DELETE This request does exactly what it is named, it removes the information of the target resource.

Now that we have a basic understanding on how request are made to the internet we can review restful routes.

A RESTful route is a route that provides mapping between HTTP verbs (get, post, put, delete, patch) to controller CRUD actions (create, read, update, delete). Instead of relying solely on the URL to indicate what site to visit, a RESTful route also depends on the HTTP verb and the URL.

WHATTTTTTT does that mean?

That means that when get a request it determines if it is a GET, POST, PATCH, PUT, or DELETE request and locates the url for it. Then it connects to the controller handling that action and url. It will then execute the code in that controller and determine what to send back to the user.

HTTP VERB ROUTE Action Used For
GET '/posts' index action index page to display all posts
GET     '/posts/new'       new action   displays create post form                        
POST '/posts'   create action creates one post
GET     '/posts/:id' show action   displays one post based on ID in the url
GET '/posts/:id/edit' edit action displays edit form based on ID in the url
PATCH '/posts/:id' update action modifies an existing post based on ID in the url
PUT '/posts/:id' update action replaces an existing article based on ID in the url
DELETE '/posts/:id' delete action deletes one post based on ID in the url
#in your application controller
get '/posts' do
    @posts = Post.all
    erb :'/posts/index'
  end

The above getting a variable @posts to all of the posts you created in your database and rendering the /posts/index page with that information. @posts will now be available on your posts/index page as a variable that you can use to iterate over and display on the page.

#in your application controller
  get '/posts/new' do
    erb :'/posts/new'
  end

The above renders the /posts/new page so that the user can fill out a form which will look similar to this:

#in your views/posts/new
<form action="/posts" method="POST" >
  <input type="text" name="content" placeholder="content"><br/>
  <input type="submit" value="Create">
</form>

The form is making a POST request with the params named "content" that you can use to create a new post, like this:

#in your application controller
  post '/posts' do
    Post.create(content: params[:content])
    redirect '/posts'
  end

To make a patch/put request you will need to find the item that you are trying to edit and then use a form to make updates and make the patch/puts request.

You would find the item that you are trying to edit by using the .find method mentioned in a past post, like this:

#in your application controller
  get '/posts/:id/edit' do
    @post = Post.find(params[:id])
    erb :'/posts/edit'
  end

In a past post I mentioned that Ruby forms do not allow a user to make patch, delete, or puts request and that we must require Rack::MethodOverride in our config.ru. What I meant by that is that when you make a request, it requires an extra step, to make the application believe that it is making a POST request when really it is making a patch, put, or delete request. For example, our edit form will look like this:

<form action="/posts/<%= @post.id %>" method="post">
  <input id="hidden" type="hidden" name="_method" value="patch">
  <input type="text" name="content" value= "<%=@post.content %>">
  <input type="submit" value="update"/>
</form>

Rack::MethodOverride is like a Ruby helper that allows us to hide the patch method under the post method so that we can make that request.

Now to make the patch request in the application controller we would do the following.

#in your application controller
  patch '/posts/:id' do
   @post = Post.find(params[:id])
   @post.update(content: params[:content])
   redirect '/posts'
 end

Lastly, to make a delete request we would provide the following:

#in your views/posts/edit
<form action="/posts/<%= @post.id %>" method="post">
  <input id="hidden" type="hidden" name="_method" value="delete">
  <input type="submit" value="update"/>
</form>
#in your application controller
 delete '/posts/:id' do
  Post.destroy(params[:id])
  redirect to '/posts'
end

Resources:
w3schools

Mozilla Developer

Wiki

Check out my #100DaysofCode Github Repo

#100DaysofCode (65 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 63 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables

Posted on by:

sincerelybrittany profile

Brittany

@sincerelybrittany

Web Developer | Software Engineer 👩🏾‍💻 | Determined | Music & Dance | #100DaysofCode | #WomenWhoCode

Discussion

markdown guide