2016-10-26 7 views
0

私のアプリでは、関連するレコードで使用されているレコードが削除されないようにしたい。たとえば、ビジネスパートナに文書(送信者または受信者)がある場合、その文書を破棄してはならず、ユーザはフラッシュメッセージを受け取る必要があります。rails 5 - before_destroy - レスキュー例外

以下のコードを使用していますが、例外が発生します。 ?flash[:notice]message:

どのようにそれが行われる必要がありますように -

before_destroy do |business_partner| 
    if Document.where(:sender_id => business_partner).exists? || 
     Document.where(:receiver_id => business_partner).exists? || 
     Annotation.where(:sender_id => business_partner).exists? || 
     Annotation.where(:receiver_id => business_partner).exists? 
     raise "#{business_partner.name} has ongoing activity and cannot be deleted." 
    end 
end 

は、いくつかの選択肢を試してみましたか

答えて

1

raiseですから、例外が発生します。

アラートでどこかにリダイレクトするためにそれを変更することができます。

redirect_to root_path, alert: "#{business_partner.name} has ongoing activity and cannot be deleted." 

ます。また、団体、dependent: :restrict_with_errorに見てみたいことがあります

:restrict_with_errorはエラーが追加されます所有者が の場合、関連付けられたオブジェクトが存在します。

+0

for ':restrict_with_error'どのようにして' flash [:notice] 'を追加して –

+0

を見つけたら、コントローラに解決策が見つかりましたか? Tx。 –

関連する問題