简体   繁体   中英

Ruby on Rails: Unfollow users

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

您可以尝试使用单数而不是复数形式的friendship_path(friendships_path)

<%= link_to "Unfollow", friendship_path(current_user.friendships.find_by_friend_id(u‌​ser.id)), :method => :delete, data: { confirm: "Are you sure?" }, class:"btn btn-md btn-danger" %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM