Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
So far we've only been able to create Comments via the Rails console. It's finally time to set up creation of Comments via the browser.
So far we've only been able to create Comments via the Rails console. It's finally time to set up creation of Comments via the browser.
This is going to work a lot like the creation of standalone models like Posts and Pages that we saw in previous courses. The difference is that each Comment will be created in the context of the Post it belongs to. The form to add a Comment will be on a Post's show
page. And we'll need a Post ID to use when saving the Comment model object to the database.
views/posts/show.html.erb
<div id="comments">
<h1>Comments</h1>
<%= render @post.comments %>
<!-- NEW CODE BELOW: -->
<h2>Add a Comment</h2>
<%= render partial: "comments/form", locals: {comment: @post.comments.new} %>
</div>
views/comments/_form.html.erb
<%= form_for [@post, comment] do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :content, "Comment" %>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
- Next we need a
CommentsController
:bin/rails generate controller Comments
controllers/comments_controller.rb
class CommentsController < ApplicationController
# No `new` action because form is provided by PostsController#show
def create
@post = Post.find(params[:post_id])
# Create associated model, just like we did in the console before
@comment = @post.comments.create(comment_params)
# We want to show the comment in the context of the Post
redirect_to @post
end
private
def comment_params
params.require(:comment).permit(:name, :content)
end
end
-
0:00
So, far we've only been able to create comments via the Rails console.
-
0:03
It's finally time to set up the creation of comments via the browser.
-
0:08
This is going to work a lot like the creation of stand-alone models like
-
0:11
posts and pages that we saw in previous courses.
-
0:14
The difference is that each comment will be created in the context of the post it
-
0:18
belongs to.
-
0:19
The form to add a comment will be on a post show page.
-
0:23
And we'll need a post ID to use when saving the comment model object to
-
0:26
the database.
-
0:28
So let's go to the show view for posts, and we're going to add a form for
-
0:32
adding a comment that belongs to this post.
-
0:35
So first, we're going to create a new header and we'll say Add a Comment.
-
0:42
And then we're going to render the form from a partial.
-
0:45
So we'll insert an ERB tag with a call to render.
-
0:50
And we're going to specify a partial render that'll be in the comments folder.
-
0:57
And it's going to be a partial named form.
-
1:00
Again no underscore needed in the name when you're calling render.
-
1:04
Then we are going to add the locals argument to render,
-
1:06
and we are going to specify that the comment variable
-
1:11
should be set to posts.comments.new.
-
1:16
This is the series of methods that you call when you want to create a new comment
-
1:21
that belongs to a particular post.
-
1:25
Close out the ERB tag and save our work.
-
1:29
Then here in the views, comments directory, we're going to
-
1:34
create another file, and this one is going to be named _form.html.erb.
-
1:41
We're going to insert an ERB tag with a call to
-
1:45
form_for, And now this part is interesting.
-
1:50
The path that we submit the form to is going to need include a post ID.
-
1:54
So we'll provide that by providing an array here, it'll be a two element array.
-
2:00
And the first element is going to be the resource that
-
2:03
our comment is nested within.
-
2:05
So that'll be post, form_for will derive the post ID from this post object.
-
2:11
The second element of the array will be the model object that's nested
-
2:15
within the outer resource.
-
2:17
So that'll be the contents of our comment variable.
-
2:21
The form_for method takes a block so we'll provide a block here.
-
2:25
We'll take the form object as a parameter to that block.
-
2:31
And then down here, we're going to need the end keyword to end the form_for block.
-
2:37
Now for the actual HTML contents of that form.
-
2:42
We'll set up div here for our first field,
-
2:44
which is going to be the comment submitter's name.
-
2:48
We'll give that a class of field.
-
2:53
Close the div.
-
2:57
And then we're going to need to call methods on the form object to set up
-
3:01
a label for this field, and the actual field itself.
-
3:05
So we'll insert an ERB tag with a call to f.label.
-
3:09
That's going to be for the main field.
-
3:13
And then another ERB tag with a call to f.text_field.
-
3:20
That'll also be for the name field.
-
3:24
Then we'll need another field for the comment content.
-
3:26
I'm just going to copy paste this first div since the code will be very similar
-
3:30
for the two.
-
3:32
But instead of working with the name attribute,
-
3:35
we're going to be working with the comment content attribute.
-
3:39
Now labeling this as content isn't going to mean much for our users.
-
3:42
So I'm going to add an extra argument here to make sure that
-
3:47
we use the text comment when labeling this content field.
-
3:52
Then instead of a call to text_field, which produces just a one line field,
-
3:57
we're going to say text_area so that the user can enter several lines of text.
-
4:02
And this will be also for the content attribute.
-
4:09
Finally, we're going to need a button for submitting the form.
-
4:12
So we'll add another a div down here that's gonna have a class of actions.
-
4:21
Oops, little typo there.
-
4:22
Div, close the div out.
-
4:26
And in that div we're going to put a form submit button, f.submit.
-
4:33
Save that and let's try going to our browser.
-
4:37
First, let's exit out of the rails console here, and run our rails server.
-
4:44
Okay, now let's reload our post show page.
-
4:48
Whoops it looks like I made a typo in my instance variable name.
-
4:53
Let's go back to our show view and the instance variable isn't posts plural,
-
4:58
it's post singular because we're only viewing one post.
-
5:02
So let's save that, reload the page.
-
5:05
Okay, and it looks like our form rendered successfully, let's try filling it out.
-
5:09
Provide a name of Jay and a comment, Hi there!
-
5:17
Submit the form, and we get another error.
-
5:20
It’s trying to submit our form to the comments controller
-
5:23
since we’re submitting a comment.
-
5:25
So let’s go back to our terminal and quit out of the rails server.
-
5:29
And we're going to run bin/rails, generate,
-
5:34
controller, Comments.
-
5:38
That'll create a new file within our controllers folder
-
5:42
named comments_controller.
-
5:44
Since it's trying to submit a form to create a new comment,
-
5:47
it's going to call an action method named create.
-
5:50
So let's define that now, def create.
-
5:56
And because we said that our form is for a resource that's nested within a post,
-
6:01
post ID is going to be part of the form submission path.
-
6:06
So we'll be able to get the post ID from params post_id.
-
6:14
We'll need to find the post with an ID matching that parameter.
-
6:21
And we'll assign the post that it finds to an instance variable named post.
-
6:27
Now we need to use that has many association to create a new comment
-
6:31
that belongs to this post.
-
6:33
So just like we did in the console before we're going to call post.comments.create.
-
6:41
We're going to read this comment's parameters
-
6:45
from a method named comment_params.
-
6:48
That method doesn't exist yet though, so we'll need to create that now.
-
6:53
We'll create a new private method, and we'll define one named comment_params.
-
7:01
Here in the comment_params method we're going to use strong parameters to get
-
7:06
the set of parameters that we want to base our comment off of.
-
7:09
So we're going to take the set of all params.
-
7:13
We're going to require the subset that apply to the comment.
-
7:19
And then we're going to permit the name and
-
7:23
content parameters from that subset.
-
7:30
That'll be returned back up here and used as arguments to the create method.
-
7:35
And we'll wind up with a comment that has the parameters specified from our form.
-
7:41
And that belongs to the post here in the post variable.
-
7:44
We'll assign the results of that to a new instance variable named comment.
-
7:51
And now that we've saved the comment for our database,
-
7:53
we're going to want to show it in our browser.
-
7:55
We want to show it in the context of the post that it belongs to.
-
7:59
So instead of the individual comment, we're going to redirect_to, the post.
-
8:06
Okay, let's rerun our rails server and then go back to the browser.
-
8:13
Reload the page and let's try creating a comment again, see if it works this time.
-
8:19
We'll use a Name of Jay and Comment content of, Hi there!
-
8:25
Submit the form, and there's our new comment, Jay says, Hi there!
-
8:32
Excellent, we're able to create comments that belong to a post now.
-
8:36
We don't have a way to update comments once they're created, though.
-
8:39
We'll look at doing that next.
You need to sign up for Treehouse in order to download course files.
Sign up