Week 3 Quiz
1. What's the difference between rendering and redirecting? What's the impact with regards to instance variables, view templates?
Rendering displays a view template that accesses the instance variables available in that action. Therefore, instance variables must be declared in that action before the render will work. Redirecting will send a new request to the browser based on the route path that was provided. In this case, the instance variables in that action are not related to the redirected view.
2. If I need to display a message on the view template, and I'm
redirecting, what's the easiest way to accomplish this?
Because the need to redirect requests a page, you need to save the message in the session.
3. If I need to display a message on the view template, and I'm rendering, what's the easiest way to accomplish this?
Because the render server forwards the client does not resend the request. If you use flash [: message], the next two pages will appear message, with
flash.now  is only valid for the current action.
4. Explain how we should save passwords to the database.
We can use the
has_secure_password method in our model to save passwords to our database. This mechanism requires us to have a
password_digest attribute, we need to create a
password_digest column in table.
5. What should we do if we have a method that is used in both controllers and views?
The method in controllers can not be called in view, it is necessary to use helper_method to unicom view and the corresponding controller, so we can use the controller’s method in view.
6. What is memoization? How is it a performance optimization?
||= operator only assigns the value on its right to the variable on its left if the variable is not true. By doing this, it will initially hit the database only once to get the stored valued.
7. If we want to prevent unauthenticated users from creating a new comment on a post, what should we do?
logged_in? Method to return a Boolean value. In view of the use of the if statement (in logged_in? As an argument).
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 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).
class Vote < ActiveRecord::Base belongs_to :voteable, polymorphic: true end class Post < ActiveRecord::Base has_many :votes, as: :voteable end
10. What is an ERD diagram, and why do we need it?
An entity-relationship diagram is a data modeling technique that creates a graphical representation of the entities, and the relationships between entities, within an information system.
ERD can quickly organize and sketched out the project's elements (user, post) and the relationship between them. The diagram can help us to see at a glance the entire framework development. And then to develop by top-down view, let us remain clear thinking.