- 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
- 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.
- 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.
- 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.
- 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
- 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.
- 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.
- 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.
- 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.
- 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.