Question

I'm setting up friendships within a RoR website. The model for it is user_id, friend_id, and pending (boolean). I followed the RailsCast on friendships for the most part, but making some changes to it too. What I have so far is that when you go to a user's page you can click Request Friendship and the code uses:

user_friendships_path(current_user, :friend_id => @user), :method => :post

This calls the create method in the Friendships controller. It automatically sets pending to true. What I want now is to have a link to Accept it which would just turn pending to false. So I am trying to set it up like

(<%= link_to "Accept", user_friendship_path(:user_id => current_user.id, :friend_id => friendship.user.id, :pending => 'false'), :method => :put %>)

I don't actually want to go to the edit page, because it just needs to set that boolean to false, so I want to call the update directly. But when I run this page, I get the error:

No route matches {:action=>"destroy", :controller=>"friendships", :user_id=>1, :friend_id=>2, :pending=>"false"}

I don't understand why. I'm not calling the destroy (that would be with :method => :delete), and there actually is a destroy method within the Friendship controller.

The resources are set up like:

resources :users do
     resources :friendships
end

And the paths from running "rake routes" are:

user_friendships GET    /users/:user_id/friendships(.:format)             {:action=>"index", :controller=>"friendships"}
user_friendships POST   /users/:user_id/friendships(.:format)             {:action=>"create", :controller=>"friendships"}
new_user_friendship GET    /users/:user_id/friendships/new(.:format)         {:action=>"new", :controller=>"friendships"}
edit_user_friendship GET    /users/:user_id/friendships/:id/edit(.:format)    {:action=>"edit", :controller=>"friendships"}
user_friendship GET    /users/:user_id/friendships/:id(.:format)         {:action=>"show", :controller=>"friendships"}
user_friendship PUT    /users/:user_id/friendships/:id(.:format)         {:action=>"update", :controller=>"friendships"}
user_friendship DELETE /users/:user_id/friendships/:id(.:format)         {:action=>"destroy", :controller=>"friendships"}

Any help would be greatly appreciated. Please let me know if you require more information.

Thanks.

Was it helpful?

Solution

The path that exists according to rake routes is

user_friendship PUT    /users/:user_id/friendships/:id(.:format)         {:action=>"update", :controller=>"friendships"}

The method you are using is put, but you aren't supplying ':id'.

There are two solutions depending on what you are trying to do:

  1. Change the method to get - that will point you to the new action
  2. Add :id to your URL builder - that will point you to the update action

Your code is leaning towards the second, but I think if you want to create a new friendship, the first would be better.

OTHER TIPS

I'm baffled as to why the URL helper is mapping to a destroy action. However there is a simpler problem: user_friendship_path expects params :user_id and :id, not :friend_id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top