Snow White & the 7 RESTful Routes

Melissa Gonzalez
Jul 22, 2017 · 10 min read
Snow White understands the key to staying healthy is getting enough REST and eating an apple a day!

All Good Things Come in Sevens?

Snow White had her 7 dwarves. A week has 7 days. Christians have 7 deadly sins. . . And programmers have 7 RESTful routes!

get “/models” => “controller#index” 
# Shows all instances of your model
get “/models/new” => “controller#new”
# Shows form to create new instance
post “/models“ => “controller#create”
# Creates new instance from new form
get “/models/:id” => “controller#show”
# Shows individual instance (id#)
get “/models/:id/edit” => “controller#edit”
# Shows form to edit specific instance
patch “/models/:id” => “controller#update”
# Updates instance from Edit form
delete “/models/:id” => “controller#destroy”
# Removes instance from database

CRUD Action: Create

get “/models/new” => “controller#new” 
# Shows form to create new instance
post “/models“ => “controller#create”
# Creates new instance from new form

#new RESTful Route

When the user sends a get request to /models/new, it triggers the #new route, which simply shows a form for the user to fill out. The code for the new method is:

def new
render “new.html.erb”
end
<%= form_tag “/models”, method: :post do %>
<div>Attribute1: <input type=”text” name=“input1”></div>
<div>Attribute2: <input type=”text” name=”input2"></div>
<div><input type=”submit” value=”Create New Instance”></div>
<% end %>

#create RESTful Route

Once a post request is sent to the /models url, the #create route is triggered. The create method in the controller file is where all the code exists to take the user-inputted parameters and save them to the database. For the above example, the code for the create method will look like:

def create
new_instance = Model.new(
attribute1: params[“input1”],
attribute2: params[“input2”]
)
new_instance.save
flash[:success] = “New Instance Successfully Saved!”
redirect_to “/models”
end
  • populates the instance with the attributes that the user filled out in the “Create New Instance” form,
  • saves the new instance, and
  • redirects the user to the main “/models” route while alerting the user that their instance was created.

CRUD Action: Read

get “/models” => “controller#index” 
# Shows all instances of your model
get “/models/:id” => “controller#show”
# Shows individual instance (id#)

#index RESTful Route

As noted in the comments, the first route will render a page that shows all instances of the model. To do this, the method just calls an array of all the instances, and saves it as an instance variable:

def index
@models = Model.all
render “index.html.erb”
end
<% @models.each do |model| %>
<a href=“/models/<%= model.id %>”><%= model.name%></a>
<% end %>

#show RESTful Route

The #show route utilizes URL segment parameters to allow us to build a single template that will display each instance in our database individually. In order for the correct page to be loaded containing the information for one specific instance, the controller method must first find the instance with the correct id, and return that instance only. The code for this looks like:

def show
@product = Product.find_by(id: params[“id”])
render “show.html.erb”
end
<h1><%= model.name %></h1>
<h2><%= model.attribute1 %></h2>
<h3><%= model.attribute2 %></h3>

CRUD Action: Update

get “/models/:id/edit” => “controller#edit” 
# Shows form to edit specific instance
patch “/models/:id” => “controller#update”
# Updates instance from Edit form

#edit RESTful Route

The job of the #edit route is simply to show a user a form to fill out. However, since we are editing an existing instance, it must first find the instance so that the app knows which instance we’re editing. The code for the controller#edit method would look like:

def edit
@model = Model.find_by(id: params[:url_segment_id])
render “edit.html.erb”
end
  • The form will be pre-filled with the existing information in the database, so the user can just edit what’s already there, or leave it alone if not all attributes will be updated.
<%= form_tag “/models/#{@model.id}”, method: :patch do %>
<div>Attribute1:
<input type=”text” name=“input1”
value=“<%= @model. attribute1 %>”>
</div>
<div>Attribute2:
<input type=”text” name=“input2”
value=“<%= @model. attribute2 %>”>
</div>
<div><input type=”submit” value=“Update this Instance”></div>
<% end %>

#update RESTful Route

Once a patch request is sent to the /models/:url_segment_id url, the controller#update route is triggered. Just like the “create” method, the “update” method in the controller file is where all the code exists to take the user-edited parameters and save them to the database. For the previous example, the code for the create method will look like:

def create
@model = Model.find_by(id: params[:url_segment_id]
@model.attribute1 = params[“input1”]
@model.attribute2 = params[“input2”]
@model.save
flash[:success] = “Instance Successfully Updated!”
redirect_to “/models/#{@model.id}”
end
  • updates all the attributes based on the user input from the Edit. Note that if the user didn’t edit a field, it will just save the exact same value.
  • saves the instance
  • redirects the user back to the #show page while alerting the user that their instance was update.

CRUD Action: Delete

#destroy RESTful Route

The route for the Destroy action is:

<%= form_tag “/models”, method: :delete do %>
<div><input type=”submit” value=“Delete”></div>
<% end %>
def destroy
instance = Model.find_by(id: params[:id])
instance.destroy
flash[:warning] = “Instance Successfully Deleted!”
redirect_to “/index”
end

No need to reinvent the wheel when you follow RESTful conventions!

Adventures in Code

My journey as an up & coming software engineer and web developer.

Melissa Gonzalez

Written by

Aspiring Web Developer. Fitness Enthusiast. Foodie. Beer Lover. Triathlete. Soon-to-be Former Research Scientist.

Adventures in Code

My journey as an up & coming software engineer and web developer.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade