Skip to content

Instantly share code, notes, and snippets.

@dcousette
Last active August 29, 2015 14: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 dcousette/7f6f207eb1ba04b63bf5 to your computer and use it in GitHub Desktop.
Save dcousette/7f6f207eb1ba04b63bf5 to your computer and use it in GitHub Desktop.
Tealeaf Course 2 Quiz: Lesson 2
Lesson 2 Quiz answers
1. Using the resources keyword exposes the following routes:
(ex. resources :posts)
GET /posts posts#index posts
GET /posts/:id posts#show post
GET /posts/new posts#new new_post
POST /posts posts#create
GET /posts/:id/edit posts#edit edit_post
PUT/PATCH /posts/:id post#update
DELETE /posts/:id post#destroy
2. REST stands for representational state transfer. It is a style of designing web applications where each compenent is viewed as a resource that can either be created, read, updated or deleted. These actions correspond with the CRUD actions for a database and also the 4 main HTTP verbs: POST, GET, PUT/PATCH, & DELETE.
3. The major difference between model-backed and non model-backed form helpers is flexibility.
A model-backed form helper method must adhere to an existing attribute in the model when used and cannot be named
arbitrarily. However, non model backed form helpers are not constrained this way and can be used to generate any type of element on a form.
4. The form_for method knows how to build the <form> element based on the object passed to it (ex. form_for @post), and the additional methods called on the form-builder object that form_for uses to create the form ( f.text_field :name, etc).
5. The general pattern we use in the controller actions that handle form submission is:
def create
@object = Class.new(object_params)
if @object.save
flash[:notice] = "Happy path. It worked."
redirect_to object_path(@object)
else
render 'new'
end
end
def update
@object = Class.find(params[:id])
if @object.update(object_params)
flash[:notice] = "Update successful"
redirect_to objects_path
else
render 'edit'
end
end
6. Rails validations get triggered at the point a save is attempted on the database. Any errors are saved on the object itself. To show the validation messages on the UI we would do the following:
<% if @object.errors.any? %>
<% @object.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
<% end %>
7. Rails helpers are methods which are used to extract repeated logic (that doesn't pertain to the applications' business logic) from views & controllers into a single location. The helper method can then be called throughout application. This is also used to keep the code DRY and is easier to maintain and make changes.
8. Rails partials provide us with a way to store common view template code in a single file. The code in this file can then be rendered in a template with one line of code (ex, <%= render 'form' %>). Partials help keep the code DRY.
9. We use partials when dealing with template code. Helpers are used when defining logic in methods to be reused throughout the application.
10. Non model backed forms are used whenever we don't have to bind a form to a model.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment