Project Challenge: Comments Form

Create a web form to take input for comments.

Problem statement

Now that you have your controller properly configured to handle comment submissions, you will need to create a form to get user comments.

For now, you need to:

  1. Create a form to take input from users. The input should be for the body attribute of your comments table.
  2. Use text_area instead of text_field. This does not have a particular functional reason, but text_area is more appropriate for taking inputs such as comments.

The form should appear in the show view for a particular link.

Important concepts

  1. We use form_withform to create forms. When called without any additional parameters, this method will send a POST request to the current page upon submission.

  2. POST requests are handled by the create action in your controller.

  3. To bind a model object to your form, use form_with with model: @instance_variable.

  4. To direct your form request to a specific URL, use form_with with url: my_path.

  5. We need nested routesCommentRoutes for comments e.g. links/2/comments. Nested routes will allow you to retrieve information such as params[:link_id] and params[:comment_params] from the same url. You pass an array to your model parameter in form_with to build a nested route.

form_with (model: [@instance_variable, @instance_variable.comments.build],

Note: This is not the entire form_with command. build is an alias for new, so using either works fine.


Implementation

Get hands-on with 1400+ tech skills courses.