Michael
Michael

Reputation: 921

Couldn't find Post without an ID blog error

I'm working on adding comments to a blog I am creating in rails.

I keep getting this error "Couldn't find Post without an ID" when trying to submit a comment.

Rails shows this:

{"utf8"=>"✓",
 "authenticity_token"=>"KOsfCNHJHo3FJMIBX6KNCV2qdyoYV6n5Rb3MNbhTX3M=",
 "comment"=>{"comment"=>"work dammit",
 "post_id"=>"post"},
 "commit"=>"Post"}

Here is the comments controller

class CommentsController < ApplicationController

    def create
        @post = Post.find(params[:id])
        @comment = current_user.comments.build(params[:comment])
        if @comment.save
            redirect_to root_path
        else
            flash.now[:error] = "Your comment did not save!"
            render '/blog'
        end
    end

def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy
end

end

This is the Posts controller

 def show
    @post = Post.find(params[:id])
    @comment = Comment.new(post_id: :post)

  end

Here is the comments form

 <%= form_for @comment do |f| %>

    <div class='comment'>
    <%= f.text_area :comment, placeholder: "Leave a comment!", size: "40 x 3" %>
    </div>
    <%= f.hidden_field :post_id %>
    <%= f.submit "Post", class: "button"%>
    <% end %>

I realize I am probably doing the same thing twice. I also dumbly called the content of the comments comment, and seem to get more errors when I change it to content.

I may have broke alot of stuff.

Upvotes: 1

Views: 866

Answers (1)

Andrew
Andrew

Reputation: 43103

You're not submitting a post_id as part of your request. Your params are wrong:

{"utf8"=>"✓",
 "authenticity_token"=>"KOsfCNHJHo3FJMIBX6KNCV2qdyoYV6n5Rb3MNbhTX3M=",
 "comment"=>{"comment"=>"work dammit",
 "post_id"=>THIS SHOULD BE A POST ID},
 "commit"=>"Post"}

This is because you've set up the comment wrong in your controller:

def show
  @post = Post.find(params[:id])
  # This is incorrect
  # @comment = Comment.new(post_id: :post)

  # This is correct
  # @comment = Comment.new(:post => @post)

  # This is better
  @comment = @post.comments.build
end

You can also fix this by specifying the post ID value in the form, if you prefer to do that instead of build it in the controller:

 <%= f.hidden_field :post_id, @post.id %>

That will insert the post_id into the hidden field for it, so it actually sends the correct value.

Then in your CommentsController you need to load the post from that id. This will be:

@post = Post.find params[:comment][:post_id]

In the case as you've shown above.

However, it would be smarter to use nested resources so you get the post_id for free from the URL. See the Rails Guide.

For these kind of basic issues I'd suggest working on your fundamental understanding of what's going on in the Rails framework. It would be worth your time to go through Rails for Zombies or the Rails Tutorial. Digging in and taking the time to really understand what REST means and how the application is loading pages by responding to requests will be very much worth your time.

Upvotes: 2

Related Questions