1. Explain how a route leads to a controller and renders a view.
A route matches a URL pattern and HTTP verb to a controller and an action. Each action is associated with a view with the same name, under a folder with the same name as the controller. If a request hits a valid route, the specified controller and action will handle the request and render the associated view file.
2. Explain the difference between a schema, a database, and a model. Give an example by relating it back to the Blog app you built in lab today.
Blog Lab: the schema.rb file in the blog app.
Blog Lab: db/development.sqlite3 file
Blog Lab: Article model
. 3. big picture/how pieces connect: A user navigates to a webpage. If a URL + HTTP Action matches a route defined in config/routes.rb
, the request gets passed on to the associated controller and action. Within the controller action, model methods may be called and instance variables are usually assigned. The appropriate view (ie the view with the same name as the controller action) is rendered using the accessible instance variables, and served back to the user.
1. What would the line resources :sharks
in config/routes.rb
do?
It would create all of the routes necessary to perform all of the CRUD operations on a model called Shark
.
2. What does rake routes
do? Explain why you think this command might be useful.
rake routes
displays all valid routes in a rails application. This might be useful in remembering URL prefixes, which methods and controllers to create, and which views are necessary.
3. How would the code below look if it was written in Rails? Please specify what the name of the file or files would be as a comment, like it is below. How are routes handled differently in Rails?
# heroes_controller.rb
class HeroesController < ApplicationController
def index
@heroes = ["Batman", "Superman", "Rogue", "Wolverine"]
end
def show
heroes = {
"Batman" => "batarang",
"Superman" => "strength",
"Rogue" => "flying",
"Wolverine" => "claws"
}
@hero = params[:hero]
@superpower = heroes[@hero]
end
end
# config/routes.rb
# Routes are separated from business logic in Rails
get '/heroes', to: 'heroes#index'
get '/heroes/:hero', to: 'heroes#show'
1. what a form builder is: A form_for
tag which uses ERB to build an HTML form and POST request. It takes in the associated model(s) as an argument.
2. what the purpose of a view is: A view is what displays all information to the user. The code in the view visually structures information in a way that should make sense to the end user.
3. what params are in the context of Rails: params are a hash that contain information passed in through the URL, and can be accessed in the controller.
4. what a migration is: A migration is a piece of ruby code that defines any changes we want to the schema.
5. what strong parameters means: With strong parameters, Action Controller parameters are forbidden to be used in Active Model mass assignments until they have been whitelisted.
6. why we use partials: DRY (don't repeat yourself) principles applied to views. Partials help us reuse code across views.
7. why we use ERB: using ruby to write HTML, makes writing frontend more efficient.