Reputation: 886
I'm not sure why this does not allow current user to unfollow users. When "unfollow" button is clicked, the page gets routed to /friendships?friend_id=2 and this error message: No route matches [DELETE] "/friendships" appears
_user.html.erb
<%= link_to "Unfollow", friendships_path(friend_id: user.id), method: :delete, data: { confirm: "Are you sure?" }, class:"btn btn-md btn-danger" %>
friendship controller
def destroy
@friendship = Friendship.find_by(id: params[:id])
@friendship.destroy
flash[:notice] = "Removed friendship."
redirect_back fallback_location: root_path
end
routes.rb
resources :friendships, only: [:create, :update, :destroy]
schema.rb
create_table "friendships", force: :cascade do |t|
t.integer "user_id"
t.integer "friend_id"
t.boolean "accepted", default: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Upvotes: 0
Views: 48
Reputation: 6253
you can try using singular friendship_path not plural form (friendships_path)
<%= link_to "Unfollow", friendship_path(current_user.friendships.find_by_friend_id(user.id)), :method => :delete, data: { confirm: "Are you sure?" }, class:"btn btn-md btn-danger" %>
Upvotes: 1