Here's the second quiz for people who are just getting started learning web-application development. This focuses more on the resources routes and related concepts in Rails.
Each question is a link pointing to my corresponding answer as i understand the concepts the today.
A2: REST (REpresentational State Transfer) is a way to maintain data persistence in stateless data-transfer protocol (which is HTTP in our case). The
resources keyword in the routes.rb file creates the most used 7 routes (listed above) in a Rails application. These routes are RESTful because they conform to the REST architectural constraints for web application development.
A3: The major difference is a model-backed helper requires, and directly works on, a ActiveRecord Model object for creation of equivalent HTML tags. This makes it very tightly integrated with the model object and it's attributes. A non-model backed helper is a more generic helper to create HTML tags and does not need any ActiveRecord Model object to work on.
A4: Because the
form_for is a model-backed form helper, it takes an ActiveRecord Model object to create an HTML form for using the model object's real and virtual attributes.
- Create/Update Model Object in Memory
- Try to save it in the database
- If the save succeeds, redirect to the relevant page (showing the saved item by itself or in a list)
- If the save fails, render the page that submitted the form again, with the error messages (stored on the in-memory object) displayed
createaction of the Category controller would look like this:
1 2 3 4 5 6 7 8 9 10
updateaction would like like this:
1 2 3 4 5 6 7 8 9 10
- Model validations are triggered when the database is accessed for update, for example: the
createmethods are called on a model object
- If there are validation errors triggered during the above operation, they are saved on the model object itself which can be accessed by calling
Note: model_obj.errors.full_messages can be used to retrieve an array of error messages
- This object cab be saved in an instance variable of the controller class and then can be used in the rendered template to display errors as shown below
1 2 3 4 5 6 7 8 9 10 11 12
A7: Rails helpers are methods that can be defined to contain repetitive logic for the presentation layer (used by the view templates). These helper methods usually go in the
app\helpers\application_helper.rb file under the Rails project folder.
A8: Rails partials are HTML snippets which need to be reused in many view templates with no or minimum modifications (that can be handled by arguments). Filenames for partials begin with an
_, for example:
_errors.html.erb. This partial can be used by calling the render method on it, as in
<%= render 'shared/errors', model_obj: @post %> where
model_obj is an argument variable name that will be used in the partial (refer to the HTML code in Answer 6 above).
A9: Partials should be preferred over helpers when too much HTML needs to be embedded. Helpers should be reserved more for the logic (parsing / reformating / calculations) than the presentation itself. Back
A10: Non-model backed form helpers should be used whenever user-input elements need to be generated for properties which are not the associated attributes of an ActiveRecord Model object.