私は現在、ruby on railで友情を破壊することに問題があります。友情は正常に作成されています(私はこれをレールコンソールでテストしました)が、友情を削除することはできません。私はちょうどエラーを取得して保持するので、既存の友情のためのチェックを追加しましたRuby on Railsの友情を破壊する
<%= form_for(current_user.friendships.find_by(friend_id: @user.id),
html: { method: :delete }) do |f| %>
<%= f.submit "Unfriend", class: "btn" %>
<% end %>
注:以下は
class FriendshipsController < ApplicationController
def create
@current_friend = User.find(params[:friend_id])
@friendship = current_user.friendships.build(:friend_id => @current_friend.id)
@friendship_2 = @current_friend.friendships.build(:friend_id => current_user.id)
if @friendship.save && @friendship_2.save
flash[:notice] = "Friend added"
redirect_to current_user
else
flash[:notice] = "Can not add friend"
redirect_to current_user
end
end
def destroy
@friendship = current_user.friendships.find_by(friend_id: params[:id]).first
if @friendship.exists?
@friendship.destroy
end
@friendship_2 = Friendship.where(user_id: params[:id], friend_id: current_user.id).first
if @friendship_2.exists?
@friendship_2.destroy
end
flash[:notice] = "Friendship destroyed"
redirect_to current_user
end
end
は友情を削除するための私の形式である:以下
は私のコントローラのコードです友情は無かったと言いましたが、これは私の現在の問題です。
ありがとうございました!あなたのビューで
「働いていない」ということを明確にすることはできますか?エラーが報告されているか、単にレコードを削除していませんか? –
@BenYレコードは削除されません。私はif文を追加私はエラーを取得したため(friendship.exists @例えば場合を?): 未定義のメソッド '最初の」nilのため:(ライン#40前後)NilClass抽出ソース: 39デフ 40 @friendshipを破壊= current_user.friendships.find_by(friend_id:41件のparams [:ID])。最初 41 42 @ friendship.destroy パラメータ: { "UTF8" => "✓"、 "_method" => "を削除」 "authenticity_token" => "1sakExihzSkzP6wrrwmTp + WW5 + GQciGnBLzdG/ALzUfVnYtBidJa6EG5mDc7AcDChJlIxMN3hcY14K + 0rGv/AQ =="、 "コミット" => "Unfriend"、 "ID" => "1"} – Nerl
それフォームを送信しているときにユーザーIDを送信しているように見えますが、そのuser_idを使用して友だちを検索しています – laertiades