This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Rendering Associated Models in Views 3:15
- Rendering Associated Models Using Partials 4:29
- Rendering Collections 1 objective
- Nested Routes and Resources 4:31
- Nested Routes and Resources 1 objective
- Creating an Associated Record 8:41
- Updating an Associated Record 6:40
- Deleting an Associated Record 3:04
Start a free Courses trial
to watch this video
![](https://videos.teamtreehouse.com/stills/TH-ActiveRecordASSNInRails-Stage2-Video4-stills-0.jpg?token=6991ea0f_5f5a980600dec6ccbdba589085ea5d5ba06e9bef)
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Muhammad sharifi
4,455 Points1 Answer
-
Lukasz Walczak
6,620 Points1 Answer
-
PLUS
caven xu
Courses Plus Student 13,400 Points0 Answers
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up