Rails Routes & Controllers

Learning Objectives

  • Explain the basics of an HTTP request-response cycle

What is a server?


  • The term ‘server’ is used to refer to both software and hardware.

What happens when you go to google.com and hit Enter?


  • This is one of the most classic interview questions in the web development industry. (The real answer includes DNSs, Load Balancers, etc.)


  • When we make our request this http gets added to the front of the URL - does anyone know what HTTP is?

HTTP Request Basics

  • Comprised of: Request Line


path a part of url

url, uri, urn https://danielmiessler.com/study/difference-between-uri-url/

  • Note that these are the only portions we are focused on today, we’ll get more into HTTP when we do Rails Lite.

HTTP Methods (CRUD)

  • POST (Create)


  • When we think about the core actions we need to be able to perform on an application resource, it’s really only these 4 things. We need to be able to read information from the DB so we can display it, add new things to the DB, update existing things, and delete things. That’s it — that’s what CRUD is.


Parts of a URL


  • Discuss how we make an HTTP request to a domain and that everything that comes after the domain is what we call the path. Often, students are confused the difference between a URL and a path. Explain how the routes we write in our app are simply paths that are relative to the domain. Let them know that we aren’t putting our apps on the internet yet so we’ll be using localhost which is a domain for our own machine's IP (which is always

RESTful Routes


  • REST is a set of design principles for making network communication more scalable and flexible.

Website vs. Web Service (API)

  • Website is meant for a human to interface with — needs content that can be rendered (HTML, CSS, Javascript, images, etc.)


  • Websites are meant for human interaction, whereas web services are meant for machine interaction

Example requests to RESTful API

  • GET /users query all users


  • Here’s an example of some requests that would hit our RESTful routes. Note that we’ve replaced the wildcard :id with actual ids. Explain how this would playout again (even if you just explained on the previous slide, doesn't hurt to quickly re-iterate with an example in front of their face)

Can these two routes co-exist?

  • GET /users/:id

Note: The URI pattern is the same, with two different wildcards. These routes essentially collide with each other as both will match the same pattern (e.g. GET /users/1 and GET /users/gerald would each be matched by both routes - there's no way for us to know whether the thing after the second slash is an id or a username immediately)

HTTP Response

  • Components


  • So we have dispatched our request, its been interpreted by our router, and now its time for our server to send back its response.

GET /users in Rails App


  • Last week we discussed the Model. As we know, the model is what handles all of our database logic

ROUTER : Takes in HTTP Requests, decides where to send them, instantiate an instance of controller and invokes an action on that controller

Controllers: Take in HTTP Requests, decides what to do with them & how to respond


Hash-like Ruby Object which contains information about the HTTP request

Three ways to pass params in an HTTP request as follows:

  • Using wildcards inside a route (e.g. /users/:id)


  • Explain that these are three ways you can pass up data with an HTTP request. The body is usually used to pass up data for a POST or PATCH request, that we want to add or update in our DB. We'll see examples of using each in the demo.

Code Demo

add gem: gem ‘better_errors’gem ‘binding_of_caller’ # only in developmentconfig/routes.rb#verb 'path' to: 'controller#action' as alias_nameget "chirps", to: "chirps#index"brrbrr -c chirps # see the only one create chirps_controller.rbcreate ChirpsController classcreate Chirps#indexdef index
chirps = Chirp.all
render json: chirps
#不能有两个render,也不可能有render json 和 redirect,那样就没意义了。
可以在rails 的method里面放debuggerdef index
chirps = Chirp.all
render json: chirps
这个时候就得在跑起来的时候去检查terminal 看debug 信息了
get '/chirps', to: 'chirps#index'
get '/chirps/:id', to: 'chirps#show'
post '/chirps', to: 'chirps#create'
patch '/chirps/:id', to: 'chirps#update'
delete '/chirps/:id', to: 'chirps#destroy'

def show
resources :name_of_resources, only:/except: [:method_name, :another_method]chirps = localhost/chirps
chirp_url(1) => localhost:3000/chirp/1
chirp_url(chirp ID 是 1 的那个instance) => localhost:3000/chirp/1
chirp_url其实是一个helper method,就是能解析object或者是数字。

Setting up Routes

  • Generate 7 standard RESTful routes for a resource:


  • Explain the various ways we can create routes in Rails.

Setting up Controllers

  • Generate a controller in terminal :

skip_before_action :verify_authenticity_token

status 422 是必须的。redirect to 就是连接controller 之间的东西。
strong params

Additional tips

  • Generate a model in terminal :

redirect new http request

render not a new request, just render pictures.

要特别小心,create中的save之后,不要用render,因为render会出问题,在浏览器回退的时候,会再提交一次表。这个用redirect to可以避免

Designer transforming into a developer