我不确定为什么这不允许当前用户取消关注用户。当"取消关注"单击按钮,页面被路由到/ friendships?friend_id = 2并且此错误消息:没有路由匹配[DELETE]" / friendships"出现
_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" %>
友谊控制器
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
答案 0 :(得分:1)
你可以尝试使用单数friendship_path而不是复数形式(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" %>