Redirect_to and render with return

 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

If I do this, I get an error like

Render and / or redirection were called several times in this action. Please note that you can only call a render or redirect, and no more than once per action. Also note that neither redirection nor rendering completes the action, so if you want to exit the action after the redirection, you need to do something like "redirect_to (...) and return."

If I use return, I take me to some other page, and even the Flash message is not displayed. How to fix it?

+5
source share
2 answers

, render redirect , , , .

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

, (, ) if, return, .

+8

and return redirect_to render,

`redirect_to @game_school  and return`  

+1

All Articles