Pavel
Pavel

Reputation: 1974

ActiveRecord::RecordNotFound Couldn't find User without an ID

I made a simple voting system for users

votes_controller:

def create
  @user = User.find(params[:id])
  @vote = Vote.create(author_uid: current_user.uid)
  @user.votes << @vote

  redirect_to root_url
end

view file:

- @users.each do |user|
  %tr
    %td= user.id
    %td= link_to user.username, user.url
    %td
      = link_to 'Like', {:controller => "votes", :action => "create" }, :method => "post"

But if I vote for user there is an error "Couldn't find User without an ID"

Hope you will help to solve this problem. If you need more information, please comment.

Thanks!

Upvotes: 1

Views: 3470

Answers (2)

rb512
rb512

Reputation: 6948

Change

link_to 'Like', {:controller => "votes", :action => "create" }, :method => "post"

to

link_to 'Like', {:controller => "votes", :action => "create", :id => user.id }, :method => "post"

Also, you're not saving your user object after creating the votes object.

Upvotes: 1

cpjolicoeur
cpjolicoeur

Reputation: 13106

You are not passing in an id param in your link_to method.

That is why the params[:id] doesnt exist when you try to look it up on the server

Upvotes: 2

Related Questions