Skip to content

Instantly share code, notes, and snippets.

@mattdvhope
Created December 12, 2013 21:19
Show Gist options
  • Save mattdvhope/7935650 to your computer and use it in GitHub Desktop.
Save mattdvhope/7935650 to your computer and use it in GitHub Desktop.
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.
For getting the resources for posts...
```
/, action: index, HTTP verb: GET (name: root)
/posts, action: index, HTTP verb: GET, name: posts
/posts/new, action: new, HTTP verb: GET, name: new_post
/posts, action: create, HTTP verb: POST
/posts/:id, action: show, HTTP verb: GET, name: post
/posts/:id/edit, action: edit, HTTP verb: GET, name: edit_post
/posts/:id, action: update, HTTP verb: PATCH/PUT
/posts/:id, action: destroy, HTTP verb: DELETE
```
2. What is REST and how does it relate to the resources routes?
- "REpresentational State Transfer"
- It means using the resource key word in routes.rb to map HTTP VERBS + URL to a specific action in the controller. The same URL, when used with a different verb (GET, POST, PATCH or DELETE), will get you to a different page.
- We use rails routes to map a new URL syntax, with the HTTP verbs, to control action. We modify existing links and forms to use new URL syntax
- The actions are mostly gone from the URLs because the HTTP methods will determine the action. The HTTP method is what really determines the action.
- The focus is upon performing state transformations upon resources
- Each model is being treated as a resource. Resource = Model
- We have one CRUD controller for each of our Models
- We use HTTP verbs (`GET`, `POST`, `PATCH`, `DELETE`) for talking to our resources, determining which of our actions are suited for each resource
3. What's the major difference between model backed and non-model backed form helpers?
- Model backed form helpers are tied to an object from the model. NMBFH's are not.
- Model backed form helpers have getter/setter methods available to them, as well as attributes (from the columns in the tables) and virtual attributes provided by the model.
- Model backed form helpers are used for CRUD actions.
4. How does form_for know how to build the `<form>` element?
- A model object is passed through it so it builds the `<form>` element based on that object's attributes.
5. 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).permit(:url, :title))
if @post.save
flash[:notice] = "Post was saved"
redirect_to posts_path
else
render "new"
end
end
```
```
def update
@post = Post.find(params[:id])
if @post.update(params.require(:post).permit(:url, :title))
flash[:notice] = "Your post was updated"
redirect_to post_path(@post)
else
render "edit"
end
end
```
6. How exactly do Rails validations get triggered? Where are the errors saved? How do we show the validation messages on the user interface?
The rails validations get triggered when we use methods like 'save' or 'create' to submit data to the database. If the data does not pass validations, the errors are attached to the model object. The model object, thus will have access to the `obj.errors.full_messages` methods to create an array that can be iterated through and printed out.
7. What are Rails helpers?
- They provide a place in which complex, "business logic" ruby code can be run (extracted from the view template/controller).
8. What are Rails partials?
- They are files (prepended with underscore) in the Views folder into which repetitive code can be extracted (for example from the `new` and `edit` files, which often have nearly identical code)
9. When do we use partials vs helpers?
- We use partials to extract repetitive template code--to DRY that code.
- We use helpers to extract complex (business logic) ruby code so that it won't have to be run in the view. They provide for "separation of concerns."
10. When do we use non-model backed forms?
- To create a form that is not binded to the model object and does not require persistance of data.
- It can be used for user-authentication, to enter search criteria, to send feedback requests, etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment