Skip to content

Latest commit

 

History

History
67 lines (47 loc) · 3.18 KB

routes_and_controllers.md

File metadata and controls

67 lines (47 loc) · 3.18 KB

Routes and Controllers

Where do the key-value pairs in the params hash come from? (Select all that apply.)

  • Query string
  • The path of the request
  • Form data from a POST/PATCH/PUT request
Answer:
  • Query string
  • The path of the request
  • Form data from a POST/PATCH/PUT request
Explanation:

All of the above!

The server returned a blank form for creating a cat. Assuming RESTful Routes, which request was made?

  • HTTP Verb: NEW Path: /cats/
  • HTTP Verb: GET Path: /cats/new
  • HTTP Verb: GET Path: /cats_controller/new
  • HTTP Verb: NEW Path: /cats_controller/
Answer:

HTTP Verb: GET Path: /cats/new

Explanation:

If you are requesting a new form, you'd be making GET request. Since this request would lead to a new cat being made, Rails convention dictates that the path would be /cats/new.

Which of the following is NOT true about controllers?

  • The controller lives for only one request response cycle.
  • The controller can render or redirect, but not both.
  • The CatsController is the only controller that can access the Cat model.
  • The controller's name is conventionally pluralized.
Answer:

The CatsController is the only controller that can access the Cat model.

Explanation:

Active Record allows us to access the Cat model in multiple controllers.

Assuming RESTful routes, a GET request to /magazines/:magazine_id/articles would do which of the following?

  • Call the articles action of the MagazinesController with magazine_id as a key in the params hash.
  • Call the index action of the MagazinesController with articles as a key in the params hash.
  • Call the index action of the ArticlesController with magazine_id as a key in the params hash.
  • Call the articles action of the MagazinesController with id as a key in the params hash.
Answer:

Call the index action of the ArticlesController with magazine_id as a key in the params hash.

Explanation:

Since the last piece of the URL is plural and this is a GET request, you can assume you are hitting the index action on the matching controller (in this case, articles). Then looking at the rest of the URL, you can see the :magazine_id, meaning that you are passing a magazine_id as a key in the params hash as part of this request.

Which of the following lines, if added to config/routes.rb, would cause the root url (i.e., "/") to run the BicyclesController's index method when visited?

  • root "bicycles#index"
  • resources :bicycles, root: index
  • root :bicycles#index
  • resources root to: #index
Answer:

root "bicycles#index"

Explanation:

Proper Rails syntax is essential! Always feel free to visit the Rails documentation if you are ever unsure of the syntax to implement something.