【问题标题】:Ruby on Rails: Unfollow usersRuby on Rails:取消关注用户
【发布时间】:2017-09-14 14:38:48
【问题描述】:

我不确定为什么这不允许当前用户取消关注用户。当点击“取消关注”按钮时,页面被路由到 /friendships?friend_id=2 并出现此错误消息:No route matches [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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    你可以尝试使用单数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" %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      相关资源
      最近更新 更多