Project Challenge: Comments Form
Create a web form to take input for comments.
We'll cover the following
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:
- Create a form to take input from users. The input should be for the
body
attribute of yourcomments
table. - Use
text_area
instead oftext_field
. This does not have a particular functional reason, buttext_area
is more appropriate for taking inputs such as comments.
The form should appear in the show
view for a particular link
.
Important concepts
-
We use
to create forms. When called without any additional parameters, this method will send aform_with form POST
request to the current page upon submission. -
POST
requests are handled by thecreate
action in your controller. -
To bind a model object to your form, use
form_with
withmodel: @instance_variable
. -
To direct your form request to a specific URL, use
form_with
withurl: my_path
. -
We need nested
for comments e.g.routes CommentRoutes links/2/comments
. Nested routes will allow you to retrieve information such asparams[:link_id]
andparams[:comment_params]
from the same url. You pass an array to yourmodel
parameter inform_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 fornew
, so using either works fine.
Implementation
Get hands-on with 1400+ tech skills courses.