0

I am trying to add the acts_as_votable to like post comments on my rails 5 app. The gem was installed fine, I added two migrations (ActsAsVotable and AddCachedLikesToVotes) and to my CommentsController I added:

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

def vote
  if !current_user.lked? @comment
    @comment.liked_by current_user
  elsif current_user.liked? @comment
    @comment.unliked_by current_user
  end
end

I then added this to my comment partial view:

<%= link_to like_post_comment_path(@post, comment), class: "like-btn", method: :put, remote: :true do %>

My error is for the above line. It's saying that:

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

The like_post_comment_path is correct and I've tried different variations between (@post, comment), (@comment.post_id, comment), (@post, @comment), etc. and nothing works!

vich
  • 11,836
  • 13
  • 49
  • 66
DianaBG
  • 1,511
  • 3
  • 10
  • 13
  • 1
    What's the url you're using to access the view? And assuming `(@post, @comment)` still doesn't work, then your @comment variable isn't getting defined. Debug to see the value of `@comment` or `comment`. – vich Jan 23 '17 at 20:05
  • Defined in the controller? I'm pretty sure it is defined, but I could be wrong because im still really new to rails. – DianaBG Jan 23 '17 at 20:21
  • It looks like it's missing comments [:id] but I'm not sure how to check if that even exists. I thought it did, since I was able to create comments as a user with no error. – DianaBG Jan 23 '17 at 20:26
  • Assuming the broken link is being served by `Comments#like`, add `binding.pry` or `byebug` (you may need to install one of those debuggers) on the first line of the method and refresh the page - this will intercept the request and will give you access to your method, so you can inspect `@post` and `@comment`. – vich Jan 23 '17 at 21:53
  • Ok, thank you. I'm testing that now and trying to use pry to find comment id – DianaBG Jan 23 '17 at 22:11
  • Sorry but im really rusty with ruby and pry. Are the commands supposed to be something like Comment.first or @comment.first ? I keep getting 'uninitialized constant' with everything I put in – DianaBG Jan 23 '17 at 22:14
  • I would take a look at `params` as well as the two instance variables you're defining in the `like` method. You can literally copy and paste `@post = Post.find(params[:post_id])` and inspect `@post`. Then try `@comment = @post.comments.find(params[:id])` and see what you get. – vich Jan 23 '17 at 22:31

1 Answers1

0

Try this

<%= link_to like_post_comment_path(comment, poste_id: @poste.id), class: "like-btn", method: :put, remote: :true do %>
El Fadel Anas
  • 1,581
  • 2
  • 18
  • 25
  • Is it the same error. I think mmichael is right comment is null , if you don't know how to debug on rails you can use binding.pry install pry gem and check the value of comment – El Fadel Anas Jan 23 '17 at 20:32
  • I've been trying to use pry but it keeps giving me a different error. – DianaBG Jan 23 '17 at 22:07