Skip to content

Instantly share code, notes, and snippets.

@madstap
Last active August 29, 2015 14:17
Show Gist options
  • Save madstap/a2607173d134740db3b0 to your computer and use it in GitHub Desktop.
Save madstap/a2607173d134740db3b0 to your computer and use it in GitHub Desktop.
Quiz for Tealeaf course 2 lesson 2
# Quiz: Lesson 2
# Some exercises from lesson 2 materials:
# 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.
# /app/config/routes.rb
resources :posts
# $ rake routes
# http verb, named_route, url , controller action
# get , posts_path, /posts , 'posts_controller#index'
# post , posts_path, /posts , 'posts_controller#create'
# get , new_post_path, /posts/new , 'posts_controller#new'
# get , edit_post_path, /posts/edit/:id , 'posts_controller#edit'
# get , post_path, /posts/:id , 'posts_controller#show'
# patch , post_path, /posts/:id , 'posts_controller#update'
# delete, post_path, /posts/:id , 'posts_controller#destroy'
# 2. What is REST and how does it relate to the resources routes?
# REST stands for REpresentational State Transfer, and is a collection of guidelines and best practices for making web apps and services. It maps resources to CRUD actions.
# 3. What's the major difference between model backed and non-model backed form
# helpers?
# Model backed ones are constrained to using only attributes of the model, while non-model backed ones can be anything.
# 2. How does form_for know how to build the <form> element?
# It creates a form based on a specific objects attributes, and uses
# SomeModel.exists?(@some_model) to decide whether is should send a post or a
# patch request.
# 3. What's the general pattern we use in the actions that handle submission of
# model-backed forms (ie, the create and update actions)?
class SomeModelController
def create # or update
@some_model.new(some_model_params)
if @some_model.save
flash[:success] = 'Great success!'
redirect_to some_model_path(@some_model)
else
render :new
end
end
end
# 4. How exactly do Rails validations get triggered? Where are the errors saved?
# How do we show the validation messages on the user interface?
# Validations are triggered when we try to save something to the database or use the valid? method.
# The errors are saved in the object at object.errors.
# By using object.errors.full_messages.
# 5. What are Rails helpers?
# Helpers are utility functions to use in views.
# 6. What are Rails partials?
# Reusable pieces of view templates.
# 7. When do we use partials vs helpers?
# Helpers are ruby functions and are used to extract logic from the views while partials are pieces of html (usually in the form of erb or haml) and are used for presentation.
# When do we use non-model backed forms?
# When we want the user to input something that is not a CRUD action on a model.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment