Homework for 11

Checkpoint Answer Key

Q1: Where do our RESTful actions belong?

A1: Controller

You always define your actions in the controller. RESTful routes are defined in config/routes.rb. The model and view do not have anything to do with RESTful actions.

Q2: Which of these is not an HTTP verb?

A2: UPDATE

With respect to CRUD, POST is used to create, GET is used to read, PUT/PATCH are used to update, and DELETE is used to delete.

Q3: Which method do we use to define all of the RESTful routes simultaneously?

A3: resources

We use the method resources in the config/routes.rb file.

Q4: Which of these is the proper format for whitelisting params for a post?

A4: params.require(:post).permit(:title, :body)

The model is the argument for the require method, and the whitelisted attributes are the arguments for the permit method. All arguments are symbols.

Q5: How would you assign an instance variable to all of the instances of a Post model?

A5: @posts = Post.all

.all is a class method, and can only be called on the model, which is singular and capitalized. .find(1..100) will only return 100 instances, and .where(title: nil) will return all posts with no title assigned.

Q6: Given the object @post = Post.find_by(title: “Hello World!”), which of these will display Hello World! on our show.html.erb page?

A6: <%= @post.title %>

We need the % sign on both sides to signify ruby code within the tag. We only need a = on the left side to render the return value of the contents within the tag.

Q7: Before we can @post.destroy to delete a post, what must we do?

A7: @post = Post.find(params[:id])

We need to find the post associated with a given id. The argument of the .find method is the id, which is an integer.

Q8: Given the route: root 'welcome#index'. What is welcome? What is index?

A8: welcome is the name of the controller and index is the action

Routes are defined as Controller#Action.

Q9: How many RESTful actions are there?

A9: 7

There are 7 RESTful actions: index, create, new, edit, show, update, and destroy.

Q10: The following code is part of 'posts/new.html.erb': <%= render 'form' %>. What will this line do?

A10: Inserts code from posts/_form.html.erb into the view

Partials are always saved with an underscore in front of the partial name. When a partial is called in the view, if only the name of the partial is written (no folder name in front), the partial rendered will be in the same folder as the view. Rendering a partials does not redirect a user to a new page. It just loads view code from a separate file.

Q11: In what situation would using a partial be a good idea (i.e. adhering to DRY principles)? Please select all that apply.

A11: Displaying nav bar that is displayed on every page, Displaying form that is used in both creating and editing a new post

Partials are used so that developers don't have to repeat view code. If a piece of view code is used on multiple pages, then using a partial is a good idea. A nav bar or a form used in multiple places are both scenarios where a piece of view code is repeated. The other two options do not have view code that is repeated.

Q12: To create a comment that is associated with a post, where a post has many comments and a comment belongs to a post, what is the bare minimum that I need to do? Please select all that apply.

A12: Add 'has_many :comments' to the Post model, Add 'belongs_to :post' to the Comment model, Run a migration to add a column 'post_id' in the Comments table in the schema, Either manually link a comment to a post upon creation or build a comment directly into a post's comments collection

All the above actions need to be taken to create a comment that is associated with a post. Adding a nested route or a form for creation is not strictly necessary because you can create the association via rails console.

Q13: Let's say we are working on an architecture app, where a house has many rooms, and a room belongs to a house. We navigate to /houses/:house_id/rooms/new to create a room that is associated with the house with ID :house_id. Which of the following is valid code for the 'create' method in the rooms controller? Please select all that apply.

A13:

# Finds correct house
@house = House.find(params[:house_id])
# Builds room associated with house with room parameters 
@room = @house.rooms.build(room_params) 
# Saves the room
@room.save 
# Even though the other two work, this is the best way to do it.

# AND
# Finds the correct house, and creates a room associated with the house.
# Create is equivalent to build and save.
@house = House.find(params[:house_id]).rooms.create(room_params) 

# AND
# Finds correct house
@house = House.find(params[:house_id]) 
# Creates a room, not associated with any house.
@room = Room.create(room_params) 
# Manually add in association by adding the newly created room to the collection of rooms
# associated with the the correct house.
@house.rooms << @room

Q14: What is the following form helper generating? <%= form_for([@article, @article.comments.build]) do |f| %>

A14: A form that will create a new comment associated with a specific article

@article.comments.build always creates a new comment, and adds in the association between the two models automatically. The last element in the list of the form helper is the model that is being created.

Q15: If I have the following line in config/routes.rb, which of these routes is valid?

resources :restaurants do 
  resources :menus 
end 

A15: /restaurants/:restaurant_id/menus/:id

This is what we call a nested resource, where :menus are nested within :restaurants. That means menus will come after restaurants in the URLs.

Q16: What is the purpose of the command 'rake db:seed'?

A16: To populate the database

rake db:seed executes any code that lives within the seeds.rb file, which usually creates instances of existing models. rake db:migrate changes the structure of the schema, and the structure of your database indirectly.

Q17: 'rake db:migrate' changes the contents of the database.

A17: False

rake db:migrate changes the structure, not the contents! To change the contents, you can add to your seeds.rb file and run rake db:seed, add instances via the rails console, or create new instances of models in the controller.

Take the portion under the PREFIX column of rake routes and append _path to the link helper. artist_album_path is your URL helper. As arguments to the URL helper, pass in objects in the order they appear in the URL. In this case, pass in the artist instance first, and then the album instance.

Q19: Check all ways that you can put data into your database. Please select all that apply.

A19: running 'rake db:seed', running 'rails console' and then creating a post via the command line, Creating a post under the 'create' method in the posts controller

Q20: Check all the situations in which you would create a new migration. Please select all that apply.

A20: Deleting a column in an existing Posts table, Adding a column in an existing Posts table, Destroying a Comments table, Creating a Post model

Creating a Post model will automatically generate a new migration. Deleting/adding columns and deleting/adding tables also have to do with changing the structure of the schema. Adding a new comment into your database changes the contents and not the structure.