2013-03-14 9 views
5
def confirm_invite_new_tutor 
    redirect_with_msg = false 
    @game_school = GameSchool.find(params[:id]) 
    existing_user_emails = params[:all_emails][:existing_user] || [] 
    new_users = params[:param_game_school][:game_school_invites_attributes] 

if existing_user_emails.present? 
     existing_user_emails.each do |existing_user| 
     // some code 
     end 
     redirect_with_msg = true 
    end 
    if new_users.present? 
     if @game_school.update_attributes(params[:param_game_school]) 
     redirect_with_msg = true 
     else 
     render :invite_tutor_form 
     end 
    end 
    if redirect_with_msg 
     redirect_to @game_school, notice: "daw" 
    else 
     redirect_to @game_school 
    end 
    end 

リターンでレンダリングします。レンダリングまたはリダイレクトを呼び出すことができるのは1回の操作につき1回だけです。また、リダイレクトもレンダリングもアクションの実行を終了させないので、リダイレクト後にアクションを終了するには、 "redirect_to(...)and return"のような処理が必要です。Redirect_toと私はこれを実行していた場合、私は</p> <p>は、レンダリングおよび/またはこのアクション内で複数回呼び出されたリダイレクトなどのエラーを取得しています

私はリターンを使用している場合は、他のページに移動し、フラッシュmsgも表示されません。 これを修正するには?

`redirect_to @game_school and return` 

の下にこれがあなたのためにあなたがコントローラでrenderまたはredirectを使用

答えて

1

は各redirect_toまたはrenderの終わりにちょうどand returnをすべき追加します渡されないことが確実でない限りレンダリングまたはリダイレクトを行います。

if new_users.present? 
    if @game_school.update_attributes(params[:param_game_school]) 
    redirect_with_msg = true 
    else 
    render :invite_tutor_form 
    end 
end 

検証はあなたが属性を更新する際に失敗した場合、あなたのコードを使用して、あなたはrender :invite_tutor_formを実行しています。しかし、コードは次のコード部分を実行し続けます。

if redirect_with_msg 
    redirect_to @game_school, notice: "daw" 
else 
    redirect_to @game_school 
end 

このエラーが発生することがあります。最も簡単な解決策は、これらの、あなたがreturnが含まれている場合はブロックの後に(他の属性を更新する、または電子メールを送信するように)より多くの処理をやっているときことに注意してくださいrender

if new_users.present? 
    if @game_school.update_attributes(params[:param_game_school]) 
    redirect_with_msg = true 
    else 
    render :invite_tutor_form 
    return 
    end 
end 

に呼び出した後returnを追加することですコードの一部が実行されません。

+2

何についてredirect_to xyz &&返信? – Lee

+1

'と'と '&&'(lol)は、Rubyにおいて、優先順位に関して2つの非常に異なる意味を持っています(http://ruby-doc.org/core-2.3.0/doc/syntax/precedence_rdoc.html参照)。この場合、 'と'を使うべきです。これは、トピックに関するもう1つの興味深い資料です。http://devblog.avdi.org/2014/08/26/how-to-use-rubys-english-and-operator-with-going-nuts/ – lucke84

8

毎回、残りのコードのどの部分を動作するように

+0

'redirect_to'でも' return'が必要ですか? – barnett

+1

はい、同じアクションで両方の2つを持つことはできません。 – jvnill

+0

'before_action'で' redirect_to'を使用した場合は例外となります。 アクションの前にリダイレクトが発生した場合は、リターンする必要はありません。 – saneshark

関連する問題

 関連する問題