0

Been trying to solve this for quite some time but can't figure out what I am doing wrong. I am simply trying to destroy a friendship between two users.

The view looks like this:

friendships/index.html.erb

<% @users.each do |user| %>

<% if user.id != current_user.id %> 
<%=h user.name %> 
<%if current_user.is_friend? user %>
<%= link_to "Destroy", friendships_path(:friend_id => user), :method => :delete %> 

<%else%>
<%= link_to "Add friend", friendships_path(:friend_id => user), :method => :post %> 
<% end %>
 <% end %>

<% end %>

friendship controller

    def index
      @users = User.all
    end

    def show
     @user = current_user
    end

 def create
   @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
   if @friendship.save
     flash[:notice] = "Added friend."
     redirect_to root_url
   else
     flash[:error] = "Error occurred when adding friend."
     redirect_to root_url
   end
 end

def destroy
  @friendship = current_user.friendships.find(params[:id])
  @friendship.destroy
  flash[:notice] = "Successfully destroyed friendship."
  redirect_to root_url
end  

I am getting this error:

No route matches [DELETE] "/friendships"

I But it looks like I have the path when I run rake routes:

     friendship GET    /friendships/:id(.:format)         friendships#show
                PUT    /friendships/:id(.:format)         friendships#update
                DELETE /friendships/:id(.:format)         friendships#destroy
Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
OXp1845
  • 483
  • 2
  • 6
  • 20
  • looks like you id isn't making it to the link. What if you replace `friendships_path(:friend_id => user)` with `"/friendships/#{user.id}"`? – froderik Dec 09 '12 at 16:41
  • also: what is a friendship? A relation between two users? The id from the user surely can't be the friendship id can it? – froderik Dec 09 '12 at 16:43
  • looks like this is a duplicate of http://stackoverflow.com/questions/4446697/why-rails-link-to-does-not-work-for-delete-action – chrispanda Dec 09 '12 at 16:57
  • That's true froderik. The friendship has: an id, user_id and a friend_id. If I try what you suggest above I will destroy the friendship with id=user.id. This is not really what I am looking for, I need something that will find my friendship with the parameter friend_id=user.id Thanks for your input though, I manage to delete something but it wasn't completely right. – OXp1845 Dec 09 '12 at 17:01
  • Not really the same problem. That one is requesting a GET method and has a lot to do with JAVASCRIPT. No Javascript in this... – OXp1845 Dec 09 '12 at 17:42

1 Answers1

3

This line

<%= link_to "Destroy", friendships_path(:friend_id => user), :method => :delete %>

should looks like

<%= link_to "Destroy", friendship_path(friendship), :method => :delete %>

(singular, not plural form)

The friendship is an instance of Friendship object, the way to get it depends on the model code. Most likely, something like

<%= link_to "Destroy", friendship_path(current_user.friendships.find_by_friend_id(user)), :method => :delete %>

should work.

dimuch
  • 12,728
  • 2
  • 39
  • 23
  • Thank you so much! I tweaked the second suggestion to: <%= link_to "Destroy", friendship_path(current_user.friendships.find_by_friend_id(user.id)), :method => :delete %> And it worked! – OXp1845 Dec 09 '12 at 18:36