Ruby on Rails blog and adding comments to posts and editing and deleting comments

Ruby on Rails blog and adding comments to posts and editing and deleting comments



I'm working on a ROR blog and have encountered some issues along the way. I'm currently learning Rails and just feel completely lost with connecting all the pieces. I've been working on my comments section for days and was finally able to create comments on posts, but I can't edit or delete them. I also referenced the SO questions below but am still running into problems.



Here's my layout:



Comment model params:



body user_id post_id



Model associations:


user.rb

has_many :posts
has_many :comments

post.rb

belongs_to :user
has_many :comments

comment.rb

belongs_to :user
belongs_to :post



routes.rb:


Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

get '/' => 'users#index'

get '/posts' => 'posts#index'

post '/posts/create' => 'posts#new'

post '/posts/edit' => 'posts#edit'

get '/signin' => 'sessions#new', as: :new_session
post '/create-session' => 'sessions#create', as: :create_session
get 'signout' => 'sessions#destroy', as: :destroy_session

resources :users

resources :posts
resources :comments
end



comments controller:


class CommentsController < ApplicationController

def index
@comment = Comment.all
end

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

def create
@comment = Comment.new(comment_params)
@comment.user_id = session[:user_id]
@postid = params[:id]
if @comment.save
flash[:notice] = "comment created."
redirect_to '/posts'
else
flash[:error] = "Error creating comment."
redirect_to '/posts'
end
end

def edit
@post = Post.find(params[:id])
end

def update
@comment = Comment.find_by_id(params[:id])
@comment.update(comment_params)
flash[:notice] = "Comment updated."
redirect_to '/posts'
end

def destroy
@comment = Comment.find(params[:comment_id])
@comment.destroy
redirect_to '/posts'
end

private

def comment_params
params.require(:comment).permit(:body, :user_id, :post_id)
end
end



Posts show.html.erb page in views/posts folder:


<%# show all posts %>

<div id="single-post">
<h1>User - <%= @post.user.username %></h1>

<h2>Post - <%= @post.body %> </h2>

<%= link_to("Edit Post", edit_post_path(@post)) %>
</br>
<%= link_to("Delete Post", @post, method: 'delete') %>
</br>
<%= link_to("Add Comment", new_comment_path(post_id: @post.id)) %>
<%#<%= link_to("Edit Comment", edit_comment_path(post_id: @post.id, comment_id: @comment.id))%>
</div>


<h3><% @post.comments.reverse.each do |c| %> </h3>
<div id="single-comment">
<h4>Comment</h4>
<h5>From - <%= c.user.username %></h5>

<h6><%= c.body %> </h6>

</br>
<%= link_to("Edit Comment", edit_comment_path(@post.id)) %>
</br>
<%= link_to("Delete Comment", comment_path(@post.id), method: :delete) %>

</div>
<% end %>

</div>



new.html.erb form in views/comments folder


<div id="comment-form">
<%= form_for @comment do |f| %>
<%= f.label :body %>
<%= f.text_area :body, class: "text-area" %>
<%= f.hidden_field :post_id %>
<%= f.submit %>
<% end %>
</div>



Again I can add comments to posts. When I hover over the edit tag on the comment I'm seeing this: localhost:3000/comments/72/edit



I see this error when I click on edit



error when trying to edit a comment



When I hover over the delete button I see this: localhost:3000/comments/72



I see this error when I click on delete



error when trying to delete a comment



I'm at the point where I'm completely lost and feel I have tried everything possible but nothing seems to work. Please help! Here's the GitHub repo as well: https://github.com/angelr1076/rails-blog






I took a quick look at your repo, but this didn't seem to include the links for editing comments. Can you check that your latest changes have been pushed.

– mikej
Sep 16 '18 at 15:22






Hi Mike. I just pushed my most recent changes to github. Thanks for taking a look at this! github.com/angelr1076/rails-blog

– Ang
Sep 16 '18 at 15:33




1 Answer
1



The First argument in form cannot contain nil or be empty is telling you that @comment in <%= form_for @comment do |f| %> is nil. This is because in the edit action of your CommentsController you are setting @post instead of @comment.


First argument in form cannot contain nil or be empty


@comment


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


nil


edit


CommentsController


@post


@comment



Change this to be:


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



For deleting a comment, the Couldn't find Comment without an ID is telling you that the value you're passing to find is nil. This is because you're trying to use params[:comment_id] instead of params[:id]. Change the destroy action to:


Couldn't find Comment without an ID


find


nil


params[:comment_id]


params[:id]


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



Update:



Also as per your code, you should change edit and delete links to below


edit


delete


<%= link_to("Edit Comment", edit_comment_path(c)) %>
<%= link_to("Delete Comment", comment_path(c), method: :delete)



You are passing @post.id which is an id of post. Instead you should pass id of the comment using the block variable from your comments.each, noticing that the .id isn't needed here because it can be inferred by Rails.


@post.id


comments.each


.id






Pavan, I did make those changes but I'm still getting the "Couldn't find Comment with 'id'=74" in the stack trace when I click edit or delete.

– Ang
Sep 16 '18 at 15:52







@Angel check my updated answer. You should change the links as well.

– Pavan
Sep 16 '18 at 15:58






That worked! Amazing how I spent over a day trying to figure this out and you got it in minutes. Thanks!

– Ang
Sep 16 '18 at 16:05






@Pavan I wanted to give the OP some explanations of the errors they were seeing so they'd be able to figure out similar problems in the future. As your answer is already accepted I've just edited your answer instead of posting a new one. I hope that's OK. Also, I spotted the OP isn't using nested routes (yet?) so shouldn't be looking up a Post using params[:post_id] in the CommentsController.

– mikej
Sep 16 '18 at 16:19



params[:post_id]


CommentsController






@mikej Absolutely ok. Nice pointers to the OP :)

– Pavan
Sep 16 '18 at 16:28



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)