Quiz: Lesson 3
1 What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates?
Rendering displays the view to be rendered on the same action, instance variables set up will be available in this view. Redirecting makes a new http request and will hit another controller action forgetting any instance variables.
2 If I need to display a message on the view template, and I'm redirecting, what's the easiest way to accomplish this?
To use the flash.
In the controller:
def some_action
flash[:error] = "Some error message"
redirect_to root_path
end
In the view (usually in the layout):
<% flash.each do |name, msg| %>
<div class="alert alert-<%= name %>">
<%= msg %>
</div>
<% end %>
3 If I need to display a message on the view template, and I'm rendering, what's the easiest way to accomplish this?
Using flash.now
In the controller:
def some_action
flash.now[:error] = "Some error message"
render :some_view
end
The view will not know the difference between flash
and flash.now
.
4 Explain how we should save passwords to the database.
As a salted hash. A hash is a one-way function that transforms the password to a big string of gibberish and makes it impossible (well, infeasible in the age of the universe) to transform back. A salt is a strign that is unique on a per password basis appended to the password to avoid rainbow table attacks. The result is called a digest, and is what is saved in the database. When the user inputs his password to log in, it goes through the same process and is compared against the saved digest.
In rails, the gem bcrypt is usually used, which gives us all this functionality just by calling the has_secure_password
method in the model, and making a password_digest
column in the database.
5 What should we do if we have a method that is used in both controllers and views?
Write it in the controller and use the helper_method :some_method
method.
6 What is memoization? How is it a performance optimization?
Memoization is remembering an expensive calculation or database lookup to avoid doing it more than once for a given input.
In ruby we use the ||=
idiom.
some_var ||= some_calc_or_db_lookup(input)
7 If we want to prevent unauthenticated users from creating a new comment on a post, what should we do?
We should make a method that redirects away if not authorized and call it before the comment#create action.
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!!current_user
end
def require_user
unless logged_in?
flash[:danger] = "Must be logged in to do that."
redirect_to root_path
end
end
end
class CommentsController < ApplicationController
before_action :require_user
def create
# Create the comment
end
end
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.
id | user_id | photo_id | video_id | post_id |
---|---|---|---|---|
1 | 4 | 12 | ||
2 | 7 | 3 | ||
3 | 2 | 6 |
id | user_id | likeable_type | likeable_id |
---|---|---|---|
1 | 4 | Video | 12 |
2 | 7 | Post | 3 |
3 | 2 | Photo | 6 |
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).
vote.rb
class Vote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
post.rb
class Post < ActiveRecord::Base
has_many :votes, as: :voteable
end
10 What is an ERD diagram, and why do we need it?
It is a diagram representing the relations between tables in our database. It is useful as guide for the implementation of the database schema.