Skip to content

Instantly share code, notes, and snippets.

@mkweick
Last active November 1, 2015 19:00
Show Gist options
  • Save mkweick/7f20b5b1faf571f4fe23 to your computer and use it in GitHub Desktop.
Save mkweick/7f20b5b1faf571f4fe23 to your computer and use it in GitHub Desktop.
Tealeaf - Course 2 - Lesson 2 Quiz
1.) index, new, create, show, edit, update, delete
GET, POST, PATCH/PUT, DELETE
GET '/photos', to: photos#index
GET '/photos/new, to: photos#new
POST '/photos, to: photos#create
GET '/photos/:id', to: photos#show
GET '/photos/:id/edit', to: photos#edit
PATCH '/photos/:id', to: photos#update
DELETE '/photos/:id', to: photos#delete
"can use as: 'something' to create a custom named route"
2.) REST is a stateless protocal that mostly uses HTTP protocal to communicate. REST uses
CRUD (create, read, update, delete) to interact with user requests. The VERB and URL
for HTTP request are used by the routes file to map the request to the controller for actions.
3.) Model back form helpers are associated with an object, either a new or current object in database.
The fields in the form must already be in the database in order to create or update.
Non-model backed forms are not associated with an object
4.) form_for builds the form based on the object associated with the form and the action in the controller
form method will be POST if new controller action and hidden _method PATCH if edit controller action.
5.) def create
if @attribute.save?(attribute_params)
redirect_to attributes_path
else
render :new
end
end
def update
if @attribute.update?(attribute_params)
redirect_to attribute_path(@attribute)
else
render :edit
end
private
def attribute_params
params.require(:attribute).permit(:name)
end
6.) Validations are triggered right before the object tries to save to the database.
The errors get saved as an attribute on the object trying to save/update.
<% if @post.errors.any? %>
<div class='row'>
<div class='alert alert-error span6'>
<h5>Please fix the following errors:</h5>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
7.) Rails helpers let us extract logic from the views into helper methods so the views contain
mainly display elements (HTML)
helper methods that will be used across mutliple views should be in application_helper.rb
8.) Rails partials are bits of HTML code that are usually used to share between mutliepl view files
Very similair or identical code in mutliple view files can be extracted to partials and called
in the view files using <% render '/partial path' %> (you can pass data into partials with
obj: something)
All partials must start with an underscore '_'
9.) partials are used when we can re-use HTML code in multiple views
helpers are used to extract logic that is currently in the view file or logic that will be reused
in multiple view files.
10.) When the form doesn't have to be associated with an object from the model. Form Tag helpers is closer
to generating an HTML form that can be used without rails involved, to a certain extent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment