Skip to content

Instantly share code, notes, and snippets.

@kangkyu
Last active August 29, 2015 14:02
Show Gist options
  • Save kangkyu/2055dd6c5141fda76ed4 to your computer and use it in GitHub Desktop.
Save kangkyu/2055dd6c5141fda76ed4 to your computer and use it in GitHub Desktop.
tealeaf academy course 2 Quiz 2
lesson 2 Quiz
1. Name all the 7 or 8 routes exposed by the resources keyword in the routes.rb file. Also name the 4 named routes, and how the request is routed to the controller/action.
name_routes action
GET posts_path posts#index
GET post_path posts#show
GET new_post_path posts#new
POST posts#create
GET edit_post_path posts#edit
PATCH posts#update
PUT posts#update
DELETE posts#destroy
(solution: # Assuming you are getting "resources :posts")
2. What is REST and how does it relate to the resource routes?
REST refers each row of a database table corresponds to each instance objects of model class. The routes are determined by methods of this class of a model.
(solution: REST stands for "Representational State Transfer" and it relies on using a stateless, client-server, cache-able communications protocol. In our case majority of the time, the HTTP protocol is used. RESTful applications use HTTP requests to create, retrieve, update, and delete data (known as CRUD operation). Thus, REST uses HTTP for all four of these actions. When we use resources routes, we are mapping the browser requests -- HTTP verbs and URLs -- to the controller actions of our app, allowing it to work seamlessly on the web.)
3. What's the major difference between model-backed and non-model-backed form helpers?
model-backed lets each instance variable of an object hold each value of the form. So when it's not saved by error, model-backed form still holds the value on the page.
(solution: the major difference is that model-backed form helpers are tied to an object. There has to be a setter method, a virtual attribute or a column in the database, available for that object when using the model-backed form helpers. We use model-backed form helpers usually when we need to create, edit, or update an object.)
4. How does form_for know how to build the <form> element?
by the source code?
(solution: By convention, form_for creates a <form> based on a specific model object. We are able to create, edit, and update that object's attributes. A <form> can be created by passing form_for a string or a symbol relating to the object we want to deal with.)
```
<%= form_for @posts do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<% end %>
```
5. What's the general _pattern_ we use in the actions that handle submission of model-backed forms (ie, the create and update actions) ?
create and update actions save the value, variable of instance object, into the corresponding database table.
(solution:
```
def create
@post = Post.new(params.require(:post).permit(:url, :title, :description))
if @post.save
redirect_to posts_path, notice: "Your Post was saved"
else
render 'new'
end
end
def update
@post = Post.find(params.require(:post).permit(:url, :title, :description))
if @post.update(params.require(:post).permit(:url, :title, :description))
flash[:notice] = "Your Post was updated"
redirect_to post_path(@post)
else
render 'edit'
end
end
```
6. How exactly do Rails validations get triggered? Where are the errors saved? How do we show the validation messages on the user interface?
When 'validates' statement on model file, the validation triggered by the POST (create or update actions) and the errors are saved to the object as variable. We show those error messages when we print it out.
(solution: Rails validations are triggered when the data submissions try to hit the database. The errors are saved on the model object. When there are validation errors, they are saved to the obj.errors -- we display the validation messages by referencing the obj.errors.full_messages method like below.. By using the full_messages method, the errors are displayed in a nice, readable sentence format.)
```
<% if obj.errors.any? %>
<h5>Please fix the following errors:</h5>
<ul>
<% obj.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>
```
7. What are Rails helpers?
method with logic for views better not be in the controller but can't be in the view erb file.
(solution: Rails helpers allow us to consolidate our application's logic and formatting so we can properly display information in the view. By declaring helper methods within the "application_helper.rb" file, we are able to use the methods in the views without convoluting the views with logic code.)
8. What are Rails partials?
we can include them by 'render' keyword to any view file. Title of it starts with underscores ('_')
(solution: Rails partials are a type of view that enable us to store and "share" common HTML files amongst all of the views. Partial files are named with an underscore at the beginning like - "_errors.html.erb".)
9. When do we use partials vs helpers?
use helpers for logic, use partials for view or display.
(solution: A partial is a view fragment with HTML code that is usually shared and used multiple times in view files. it should consists of code that is for presentation purposes only. on the other hand, helpers also reduce code duplications, they are meant to be used when there is some logic to be processed within a view. This will eliminate the views to be consumed by logic, and instead the views can remain pure for presentation usage.)
10. When do we use non-model-backed forms?
?
(solution: you can use a non-model-backed form when you want to create a form that is not tied to a model. We can think of a non-model-backed form as generating pure HTML rather than binding the form with a model object.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment