Chrizzly
Chrizzly

Reputation: 43

Couldn't find User with 'id'=#<User::ActiveRecord_Relation:0x007f616b692840>

I recently started to use rails 4 in college and I'm having troubles with deleting a user I created.

It always sais 'Couldn't find User with 'id'=#:' or 'Couldn't find User without Id'.

I looked up a few solutions as this problem seems to be pretty common but nothing worked.

My user list(Index) looks like this and at the end of every line there is the delete button.

  <% @user.each do |u| %>
  <tr>
      <td>
        <%= u.username %> <%= u.email%>  
        <%= link_to 'Delete', user_path(@user), method: :delete, :confirm => 'Are you sure?'%><br>
      </td>
  </tr>
  <% end %>

This is my destroy method

 def destroy
    @user = User.find(params[:id])
    @user.destroy
    if @user.destroy
      redirect_to root_url, notice: "User deleted."
    end
  end

And thats the root page - I suppose I made mistakes here somewhere in 'match' but I'm not sure

  resources :users

  get 'users/index'
  get 'users/new'
  get 'users/login'
  get 'users/resume'
  get 'users/userInformation'
  get 'users/update'

  root 'users#index'

  get 'index'=>'users#index'
  get 'new'=>'users#new'
  get 'login'=>'users#login'
  get 'resume'=>'users#resume'
  get 'userInformation'=>'users#userInformation'
  get 'update'=>'users#update'

  match 'users/:id' => 'users#index', :via => :delete

(I'm using Rails4)

Thanks in advance!

Chrizzly

Upvotes: 0

Views: 761

Answers (2)

Cody Caughlan
Cody Caughlan

Reputation: 32748

You need to iterate over your collection and pass each instance of that collection into your link handler:

<%= link_to 'Delete', user_path(u), method: :delete, :confirm => 'Are you sure?'%>

Right now you are passing in @user into your user_path so you're passing in the whole collection, which in this case is an ActiveRecord Relation, not the individual User instance.

Upvotes: 1

user1742188
user1742188

Reputation: 5133

Maybe try users_path instead of user_path. You can find all the routes and their helpers by typing rake:routes in the command line.

Upvotes: 0

Related Questions