-1

I am using the acts_as_votable plugin and I keep getting this error:

No route matches {:action=>"upvote", :controller=>"posts", :id=>nil} missing required keys: [:id]

I am following the answer from this SO question: acts_as_votable thumbs up/down buttons

I tried adding .id to the view as suggested here: Getting a strange error on my routes, "missing required keys" rails 4 but then I get this error:

undefined method `id' for nil:NilClass

here is my code:

post_controller:

def upvote
  @post = Post.find(params[:id])
  @post.liked_by current_user
  redirect_to @post
end

routes:

resources :posts do
  member do
    get 'like' => 'posts#upvote', as: :upvote
  end
end

view:

<% @posts.each do |post| %>
  <%= post.user.name %><BR><BR>
  <%= post.post_content %><BR><BR>
  <%= link_to "like", upvote_post_path(@post), method: :put %>
<% end %>
Community
  • 1
  • 1
MandyM
  • 87
  • 3
  • 11

1 Answers1

2

You should have:

<%= link_to "like", upvote_post_path(post), method: :put %>

The error occurs because you're supposed to use post block variable, while you try to use @post instance variable, which is unset and thus evaluates to nil.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • Thank you! When I get to 50 rep i will add a note to the original question I referenced in case others run into this problem. I will choose your answer in 10 minutes when it lets me. – MandyM Oct 29 '14 at 14:31