Skip to content

Instantly share code, notes, and snippets.

@jamesyang124
Last active December 26, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesyang124/7157288 to your computer and use it in GitHub Desktop.
Save jamesyang124/7157288 to your computer and use it in GitHub Desktop.

Rails 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.
  helpers         Http Verb   URI pattern       Controller#action
  posts_path      GET         /posts            post#index
  new_post_path   GET         /posts/new        post#new
  post_path       GET         /posts/:id        post#show
  edit_post_path  GET         /posts/:id/edit   post#edit
                  POST        /posts            post#create
                  PUT         /posts/:id        post#update
                  PATCH       /posts/:id        post#update
                  DELETE      /posts/:id        post#delete

  1. What is REST and how does it relate to the resources routes?
  • Representation state transfer. It's a pattern to be stateless for server side, clientt held in the session state and server provides resources to serve with. WHen we use resource to route, it maps to CRUD-liked methods provide by rails which are create, index, show, update, destroy, edit, and new.
  1. What's the major difference between model backed and non-model backed form helpers?
  • Non-model backed form helpers essentailly transfer tags to HTML format. It also have to specify original HTML attributes such as the action and method for the form. Model backed helpers provides a binding between HTML tag and Model object. The action and method HTML attribute has been set up by rails default.
  1. How does form_for know how to build the <form> element?
  • form_for is a helper to build the form by passing an model object. It will decide whether action to /posts [create] or /posts/:id [update] by its input model object.
  1. What's the general pattern we use in the actions that handle submission of model-backed forms (ie, the create and udpate actions)?
  def create 
    @post = Post.new(params.require(:post).permits(:with, :virtual, :attributes))
    if @post.save
      redirect_to post_path @post
    else
      render 'posts/new'
    end
  end

  def update 
    @post = Post.find(params[:id])
    
    if @post.update(params.require(:post).permits(:with, :virtual, :attributes))
      redirec_to post_path @post
    else
      render 'posts/edit'
    end
  end
  1. How exactly do Rails validations get triggered? Where are the errors saved? How do we show the validation messages on the user interface?
  • Validation is triggered when .save, .create, or other ways to submit the data to database. If we indirect to submit data through the assocaition, the validation individually get triggered for each object associated with. So it might have case that a join table save a record but failed to pass the validation and unsaved for its foreign_key's objects. The errors saves to errors hash, we can iterately call model_object.errors.fulll_messages to print out the error messages.
  1. What are Rails helpers?
  • Helpers is an abstraction from the view template routing, model logic, and data format. It more focus on how to present the data information from the model object to the view templates, and the routing policy for transferring to another view.
  1. What are Rails partials?
  • Partials is an abstraction from extracting similar template codes that prouduce HTML tags to a single file. It mainly address the HTML layout issues for View template.
  1. When do we use partials vs helpers?
  • Partial for view, html output. Helpers is for the model and routing logic. Developers can achieve the 'Don't repeart yourself' in View level by this pattern.
  1. When do we use non-model backed forms?
  • Non-model backed form suites for the case when model objects does not tied with. The non-model backed form is purely for generating the HTML code and less binding with the model object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment