開発環境ではなぜかエラーは出なかったのですが、Railsアプリをデプロイし、初期データを入力、登録すると「DoubleRenderError」が発生してしまいます。
production.log AbstractController::DoubleRenderError (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".):
コントローラー内の同じメソッドにrenderとredirectを混在してはいけません!と言われていてand returnでメソッドから抜けてください。と言われているのだと思います。
対象のアクションが記述されているcontrollerは以下になります。(ごちゃごちゃしているので問題部分を抜粋しました。)
customers_controller.rb def create x = @customer_form.customer.home_address @i = x.city if @i =~ /市$/ city_crear elsif @i =~ /区$/ city_crear elsif @i =~ /町$/ city_crear elsif @i =~ /村$/ city_crear else flash.now.alert = '市、区、町、村のみ入力してください。' render action: 'new' and return end end def city_crear if XXXXXXX else reg_success end end def reg_success @customer_form = Visitor::CustomerForm.new @customer_form.assign_attributes(params[:form]) @change = @customer_form.customer if @customer_form.save if @change.hogehoge == 'aaa' @change.hogehoge = 'bbbbb' @change.save end flash.notice = '新規に登録しました。' redirect_to :visitor_root #(ここにand returnを追記??) user = @customer_form.customer ThanksMailer.thank_you(user).deliver_now else flash.now.alert = '入力に誤りがあります。' render action: 'new' and return end end
このような感じでcreateメソッドからcity_crearメソッドを呼び出し、さらにreg_successメソッドを呼び出し、実行しています。
エラー「We're sorry, but something went wrong.」は発生していますが、データの登録はできていました。
想定する動作は、データを登録した後でvisitor_rootの画面を表示し、コメント「新規に登録しました。」を表示し、お礼のメールを送信することを想定していました。
冒頭で記述したとおりproduction.logにDoubleRenderErrorエラーが出ているので「redirect_to :visitor_root」でエラーが発生しているのかと思います。
疑問なのが、「redirect_to :visitor_root」の後にand returnを追記するだけで解決出来るのでしょうか?
考え過ぎかもしれませんがメソッド内でメソッドを呼び出すと、renderかredirectを1回使用したことになったりするのでしょうか?
どなたかご教示のほど宜しくお願いいたします。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/20 13:33