Created
December 20, 2013 23:33
-
-
Save mattdvhope/8063343 to your computer and use it in GitHub Desktop.
Tealeaf Quiz 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates? | |
-- Rendering shows the view template. It is able to use the instance variables from the action that has the same name. Redirecting sends a new request to the browser. It uses the route path that is typed in (and the object's id if necessary); the instance variables in the action are not related to the view to which the action redirects. | |
2. If I need to display a message on the view template, and I'm redirecting, what's the easiest way to accomplish this? | |
-- Use a flash message in the controller, which will display the message when you click into the template (when you've performed the action that has the flash message in it). | |
3. If I need to display a message on the view template, and I'm rendering, what's the easiest way to accomplish this? | |
-- Use a flash message in the controller, which will display the message when you click into the template (when you've performed the action that has the flash message in it). (same as for redirecting) | |
4. Explain how we should save passwords to the database. | |
-- In the model, use the 'has_secure_password' method. This will enable the creation of a 'one-way hash' which is an unintelligble version of the person's password saved into the 'password_digest' column of the 'users' table. In order for this to work, the bcrypt gem (correct version) must be installed into our application (and 'bundle install --development mode' run). Be sure to validate the presence of the password within the user model. | |
5. What should we do if we have a method that is used in both controllers and views? | |
-- In the application_controller.rb, we should type in 'helper_method [--method names (in this controller)--]'. | |
6. What is memoization? How is it a performance optimization? | |
-- It uses the 'or-equals' operator `||=`. Often used in `application_controller.rb` for the `current_user` method like this... | |
`@current_user ||= User.find(session[:user_id]) if session[:user_id]` | |
.., it allows us to hit the database only once whenever the 'current_user' method is called. If the @current_user variable is not nil, will hit the database, but once it is 'not nil' the DB will not need to be hit again for as long as you are on that template whenever the 'current_user' method is called. Hitting the DB too much slows down the app. | |
7. If we want to prevent unauthenticated users from creating a new comment on a post, what should we do? | |
-- Use the `logged_in` and `require_user` methods (in `application_controller.rb`) in conditionals (in the view templates). If user is not `logged_in?` then the `require_user` method will inform the user via an error message that the user must be logged in in order to perform that action. We will also use the `require_user` method in some of the controllers (at the top with the `before_action` method). | |
8. Suppose we have the following table for tracking "likes" in our application. How can we make this table polymorphic? Note that the `user_id` foreign key is tracking who created the like. | |
-- Make a table with a composite key composed of the columns `likeable_type` and `likeable_id`. Models that are 'objects' (not 'subjects') of the like model (i.e., are liked--receiving the action of 'like') must have a polymorphic relationship with the like model. The 'user' model has a simple `has_many` relationship with the 'like' model (not polymorphic). | |
9. How do we set up polymorphic associations at the model layer? Give example for the polymorphic model (eg, `Vote`) as well as an example parent model (the model on the 1 side, eg, `Post`). | |
-- examples... | |
``` | |
# Polymorphic association at the Like model layer | |
class Like < ActiveRecord::Base | |
belongs_to :likeable, polymorphic: true | |
end | |
``` | |
``` | |
# Polymorphic association on a parent model layer (Video) | |
class Video | |
has_many :likes, as: likeable | |
end | |
``` | |
10. What is an ERD diagram, and why do we need it? | |
-- An ERD is an 'Entity Related Diagram'. It gives a visual picture of the tables (with their respective columns--their schema) and of these tables' relationships with the other tables. For example, in a 1:M relationship, the "1" table would have a line going from its foreign key column (of the "M" table) to the "M" table's id column. It is useful as a continual reference when during development. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment