Course 2 Lesson 2 Quiz
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.
Index, Show, New, Create, Edit, Put, Patch, Destroy
Named Routes(replace controller with any object, which has a controller)- new_controller, edit_controller, controller, controllers
The named routes are used by issuing a redirect_to the named route and affixing _path and any relevant :id to the route such as: post_path(@post).
2. What is REST and how does it relate to the resources routes?
REST is used to map the various http requests to actions in the controller. The routes define which REST actions will correspond to the elements in the controller.
3. What’s the major difference between model backed and non-model backed form helpers?
A model backed helper is only exposed to the attributes, virtual or otherwise of a given object. It cannot create new information for that object unless the appropriate associations exist to set that value.
4. How does form_for know how to build the <form> element?
Rails’ form_for is the more functional equivalent of the html form element.
5. What’s the general pattern we use in the actions that handle submission of model-backed forms (ie, the create and update actions)?
Validate the parameters using a strong parameters method. Instantiate an object using the validated parameters. Attempt to save or update the object. If successful notify the user and redirect to a show action. If it failed render the form with the relevant error messages.
6. 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 defined at the model layer. They are triggered by a method call in the controller. Any errors are saved on the object being validated in an array format. Because of the array structure of the errors, it is possible to iterate through them displaying each error message. In our experience we make this easier by utilizing the full_error_message method on the errors array.
7. What are Rails helpers?
Helpers are methods we want to expose to the views, without defining them in the views themselves. This allows a common method to be used across multiple views.
8. What are Rails partials?
Partials similar to helpers are pieces of code exposed to the views that contain common code between views. Instead of methods, partials contain html and ruby elements that display elements in the browser.
9. When do we use partials vs helpers?
Partials are used when we need to display html elements, helpers contain business logic.
10. When do we use non-model backed forms?
Non-model backed forms are used to create forms that aren’t linked to specific objects.