Annette
Annette

Reputation: 317

Couldn't find Post without an ID / ActiveRecord::RecordNotFound

I know this is a very basic solution however, I am just not seeing it right now. I am getting a 'Couldn't find Post without an ID' error within CommentsController#create.

I created a 'New Comment' button under the post which should then redirect to the comment form. From there once a user inputs their comments and clicks the 'Create Comment' button the comment should be displayed under the original post. Thank you in advance.

Comments Controller

Class CommentsController < ApplicationController
  before_filter :authenticate_user!

  def new
  @comment = Comment.new
  end


  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create(params[:comment].permit(:commenter, :body))

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @post, notice: 'Comment was successfully created.' }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end

  end

def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])
    @comment.destroy
    redirect_to post_path(@post)
end

def show
  @comment = Comment.new
end

end

Post/Show

<div class="row">
    <div class="col-md-offset-4 col-med-8">
        <div class="panel panel-default">
        <div class="panel-heading center">

        <% if @post.image.url %>
            <%= image_tag @post.image.url(:medium) %>

        <% elsif @post.video.url %>
            <%= video_tag @post.video.url(:medium), controls: true, type: "video/mp4" %>
        <% end %></br>


        <p>
           <strong>Likes:</strong>
           <%= @post.get_likes.size%>
        </p>


        <%=link_to image_tag('like.jpg', :border => 0), likes_post_path(@post) %>    
        <%=link_to image_tag('unlike.jpg', :border => 0), dislikes_post_path(@post) %>


        </div>
        <div class="panel-body">
        <p><%= @post.description %></p>

        <% user = @post.user %>
        <p><strong><%= link_to(user.name, user_path(user)) if @post.user %></strong></p>

        <%= link_to 'New Comment', new_comment_path, class: "btn btn-danger btn-sm active" %></br>


        <br><% if @post.user == current_user %>
            <%= link_to edit_post_path(@post) do %>
            <span class="glyphicon glyphicon-edit"></span>
            Edit
          <% end %>
        <% end %>
        <%= link_to 'Back', posts_path %>

            </div>
        </div>
</div>

Comments / _form

<%= form_for [@post, Comment.new] do |f| %>
  <% if @comment.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

      <ul>
      <% @comment.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


  <div class="field">
    <%= f.label :body %><br>
    <%= f.text_area :body %>
  </div>
  <div class="actions">
    <%= f.submit class: "btn btn-danger btn-sm" %>
  </div>
<% end %>

Routes

 resources :comments
 resources :posts do
    resources :comments
end

Comments / Show

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

<p>
  <strong>Post:</strong>
  <%= @comment.post_id %>
</p>

<p>
  <strong>Body:</strong>
  <%= @comment.body %>
</p>

<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>

Rake Routes

post_comments GET     /posts/:post_id/comments(.:format)          comments#index
                         POST    /posts/:post_id/comments(.:format)          comments#create
        new_post_comment GET     /posts/:post_id/comments/new(.:format)      comments#new
       edit_post_comment GET     /posts/:post_id/comments/:id/edit(.:format) comments#edit
            post_comment GET     /posts/:post_id/comments/:id(.:format)      comments#show

Upvotes: 2

Views: 5810

Answers (2)

vich
vich

Reputation: 11896

I recommend making the following changes:

Routes:

Remove the first resources :comments. Leave the following:

resources :posts do
  resources :comments
end

Posts/Show view:

You should be using new_post_comment_path instead of new_comment_path. Run rake routes to see why.

CommentsController#new:

Define @post in your new action:

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

Finally, in Comments/_form:

Change <%= form_for [@post, Comment.new] do |f| %> to <%= form_for [@post, @comment] do |f| %>. Although I believe <%= form_for @comment do |f| %> should work.

I recommend going through the Rails Guides for additional information and explanations.

Upvotes: 4

Vasseurth
Vasseurth

Reputation: 6476

I believe that in def new of the comments controller, you also need to set @post, not just @comment

Upvotes: 0

Related Questions