Quiz: Week 2

Week 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.

* 7 routes: *

resources :posts, except: [:destroy]

* 4 named routes: *

posts

new_post

edit_post

post

* The Rails router recognizes URLs and dispatches them to a controller's action. *

  • If you require postspath with GET, the request routed to postscontroller.rb/index

  • If you require postspath with POST, the request routed to postscontroller.rb/create

2. What is REST and how does it relate to the resources routes?

By default, Rails creates routes for the seven default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the :only and :except options to fine-tune this behavior.

  • The :only option tells Rails to create only the specified routes.

  • The :except option specifies a route or list of routes that Rails should not create.

3. What's the major difference between model backed and non-model backed form helpers?
<%= form_for @category do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
<% end %>

Only specifications is been defined in obj, model backed form helpers can be used. The non-model backed form do this manually, this problem does not exist.

4. How does form_for know how to build the
element?
<%= form_for @category do |f| %>
  <%= f.label :name %>
  <%= f.text_field :name %>
<% end %> 

The variable f yielded to the block is a FormBuilder object that incorporates the knowledge about the model object represented by :person passed to form_for. Methods defined on the FormBuilder are used to generate fields bound to this model.

5. What's the general pattern we use in the actions that handle submission of model-backed forms (ie, thecreate and udpate actions)?
if @post.update(params.require(:post).permit!)   
  flash[:notice] = "The post updated."
  redirect_to posts_path
else
  render :edit
end 
6. How exactly do Rails validations get triggered?
[7] pry(#<PostsController>)> @post
=> #<Post id: nil, url: "", title: "", description: "", created_at: nil, updated_at: nil, user_id: nil>

[8] pry(#<PostsController>)> @post.errors.any?
=> false

[9] pry(#<PostsController>)> @post.save
   (0.1ms)  begin transaction
   (0.0ms)  rollback transaction
=> false

[10] pry(#<PostsController>)> @post.errors.any?
=> true 
Where are the errors saved?

Active Model Errors provides a modified Hash that we can include in our object for handling error messages errors.

[11] pry(#<PostsController>)> @post.errors
=> #<ActiveModel::Errors:0x007f804c733d58
 @base=
  #<Post id: nil, url: "", title: "", description: "", created_at: nil, updated_at: nil, user_id: nil>,
 @messages=
  {:title=>["can't be blank"],
   :url=>["can't be blank"],
   :description=>["can't be blank"]}> 
How do we show the validation messages on the user interface?

Use render to request the submit page(new or edit), prevent the loss of instance variables.
Write the if statement in the page, if obj.errors.any? is true, the validation messages would be show.

<% if obj.errors.any? %>
  <h4><%= pluralize(obj.errors.count, "error") %> prohibited this
      post from being saved:</h4>
  <div class="alert alert-error">
    <ul>
      <% obj.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %> 
7. What are Rails helpers?

Write some methods used in application_helper.rb need, call these methods in the view, to deal with the content view of. For example:

def fix_url(str)
  str.starts_with?('http://') ? str : "http://#{str}"
end 
8. What are Rails partials?

If a chunk of code is used in many place, write it to a independent local, then in the original location to visit this chunk with render. We can simplify the code, keep DRY.

9. When do we use partials vs helpers?
  • partials — for DRY

  • helper —— format data

10. When do we use non-model backed forms?

Because the model backed forms to rely object variable, when we use form to submit non-database data, such as cookies, session, we can only use non-model backed forms.

GitHub:

https://github.com/vivijie/tealeaf_week_1

Heroku:

http://tealeaf-neil.herokuapp.com/

Neil Wang

产品经理。喜欢摄影,滑板,电影。微信公众号:NeilOnly