2017-05-08 3 views
0

エラーを発生させて、レジストレーションコントローラの編集ページを表示しようとしています。しかし、エラーが発生したときにページがフリーズし、このエラーが発生します。私はraise ActiveRecord:Rollback私が望むようにそれが実際に変更をロールバックが、それは続行し、編集ページをレンダリングしていないことを打ったときRegistrationsControllerのテンプレートが見つかりません#update

No template found for RegistrationsController#update rendering head :no_content 
Completed 204 No Content in 698ms (ActiveRecord: 2.3ms) 

は、ここに私のコントローラのアクション

def update 
    resource.transaction do 
     super do |user| 
     if membership_params.present? 
      ToggleAlertEmails.perform(user: current_user, params: membership_params) 
     end 

     if user.errors[:current_password].present? 
      raise ActiveRecord::Rollback 
      redirect_to edit_user_registrations_path 
     end 
     end 
    end 
    end 

です。どうすればこれを達成できますか?トランザクション外

+0

この回答をスタックオーバーフローから参照することはできますが、質問を読むことを忘れないでください。状況はあなたのものと似ています。 [http://stackoverflow.com/questions/38460895/possible-to-render-and-raise-exception-in-rails-controller] – kparekh01

答えて

0

移動redirect_to edit_user_registrations_path、ロールバックが実行されたときにのみリダイレクトするように(下の例ではerrorを)フラグを使用して、このような何か:

あなたが好むなら
def update 
    error = false 

    resource.transaction do 
    super do |user| 
     if membership_params.present? 
     ToggleAlertEmails.perform(user: current_user, params: membership_params) 
     end 

     if user.errors[:current_password].present? 
     error = true 
     raise ActiveRecord::Rollback 
     end 
    end 
    end 

    redirect_to edit_user_registrations_path if error 
end 

かは、フラグを回避し、再びuser.errors[:current_password].present?を使用します。

redirect_to edit_user_registrations_path if user.errors[:current_password].present? 

updateアクション(たとえば、update.html.erb)のためのビューがありませんので、あなたが掲示特定のエラーがあるので、あなたが1つを作成または指定する必要がありますが、別のレンダリング/リダイレクトrender/redirect_to

あなたは常にeditにリダイレクトしたい場合は、最終的ifを回避し続けるredirect_toのみ:

redirect_to edit_user_registrations_path 

あなたは、どちらupdate別のアクションにリダイレクトするか、それがある(別のビューをレンダリングしたい場合でもedit)は、ロールバックがない場合、if/else完全なステートメントを使用:ちょうどいいえ、覚え

if user.errors[:current_password].present? 
    redirect_to edit_user_registrations_path 
else 
    redirect_to other_action_path 
end 

をあなたが選択したシナリオでは、レンダリング/リダイレクトをトランザクション外に追加する必要があります。

関連する問題