Redirect_to and render with return

后端 未结 2 865
长发绾君心
长发绾君心 2021-01-18 11:03
 def confirm_invite_new_tutor
    redirect_with_msg = false
    @game_school = GameSchool.find(params[:id])
    existing_user_emails = params[:all_emails][:existing_         


        
相关标签:
2条回答
  • 2021-01-18 11:41

    everytime you use render or redirect in a controller, no part of the remaining code should have a render or redirect unless it's sure that it won't be passed. using your code

    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 validation fails when you update the attributes, you're running render :invite_tutor_form. But the code will keep on running the next part of the code which is

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

    so you get that error. The simplest solution is to add a return after the call to 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
    

    Please do note that when you're doing more processing (like updating other attributes, or sending emails) after the if block that contains return, those part of the code will not be executed.

    0 讨论(0)
  • 2021-01-18 11:49

    add justand return at the end of each redirect_to or render like below

    `redirect_to @game_school  and return`  
    

    This will work for you

    0 讨论(0)
提交回复
热议问题