Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Active Record Associations in Rails Using Associations in Your App Updating an Associated Record

No route matches {:action=>"edit", :controller=>"comments", :id=>nil, :post_id=>"2"}, missing required keys: [:id]

When I add the following inside 'app/views/comments/_comment.html.erb':

<%= link_to "Edit", edit_post_comment_path(@post, comment) %>

I got an error as written above. i've checked my routes:

resources :posts do
    resources :comments

Not sure how to fix this, help?

3 Answers

Jay McGavren
Jay McGavren
Treehouse Teacher

Looks like comment.id is nil; it needs to be set so that the generated path also contains the comment ID.

What is comment set to? It might help if we saw the code for your controller and/or your posts/show.html.erb...

Hi jay,

Thanks for you response, here's my post_controller.rb

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all

  # GET /posts/1
  # GET /posts/1.json
  def show

  # GET /posts/new
  def new
    @post = Post.new

  # GET /posts/1/edit
  def edit

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }

    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :body)

and here's my show.html.erb:

<p id="notice"><%= notice %></p>

  <%= @post.title %>

  <%= @post.body %>

<div id="comments">
  <!-- render realizes you're passing it a collection, so it's as if you're passing it as the collection: argument.  -->
  <%= render @post.comments %>
  <!-- adding comment form that will render from partial app/views/comments/_form.html.erb -->
  <h2>Add a comment</h2>
  <%= render partial: "comments/form", locals: {comment: @post.comments.new} %>
  <!--  same thing: we can loop through a collection and render a partial for each using render's collection: argument: as: argument takes symbol with name of local variable to assign
  <%= render partial: "comments/comment", collection: @post.comments, as: :comment %>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>
Jay McGavren
Jay McGavren
Treehouse Teacher

What does the comment variable contain? The best way to view it is probably using the Rails logger. Try adding <% Rails.logger.debug comment.inspect %> to your _comment.html.erb partial. Or add logger.debug @post.comments.inspect to your show method in the posts controller. We're looking to see if all the post's comments have their id attribute set.

Hey fazrul kamisan, Removing the commented-out text in the show template file should fix your issue. Seems like the code still runs even with with the HTML comment-out syntax when there is ruby code embeded <!-- nope! -->