Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 709133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:28:12+00:00 2026-05-14T04:28:12+00:00

I know, if i will write redirect_to instead of render :action, i’ll lose my

  • 0

I know, if i will write “redirect_to” instead of “render :action”, i’ll lose my @object’s errors.

My code:

  def play
    @room = params[:id][0,1]
    @current_players = CurrentPlayer.all(:conditions => {:room => @room})
  end

  def join
    @new_player = CurrentPlayer.new(:user_id => current_user.id, :team_id => nil, :room => params[:room], :mode => params[:mode], :ip => request.remote_ip)
    if @new_player.save
      @game = create_game(params[:room], params[:mode])
      if @game
        flash[:notice] = 'Game created'
        return (redirect_to :action => "game_details", :recent => true)
      else
        flash[:error] = 'Game not created'
      end
   else
     return render(:action => 'play')
   end
    redirect_to_back
  end 

If a user will click a “join” link in play.html.erb, Rails will send request to “join” action, then, if there were errors, i need to show them to user.

But i can’t write just redirect_to, and my code is now:

  def join
    @new_player = CurrentPlayer.new(:user_id => current_user.id, :team_id => nil, :room => params[:room], :mode => params[:mode], :ip => request.remote_ip)
    if @new_player.save
      @game = create_game(params[:room], params[:mode])
      if @game
        flash[:notice] = 'Game created'
        return (redirect_to :action => "game_details", :recent => true)
      else
        flash[:error] = 'Game not created'
      end
   else
    # == Very ugly and not DRY (just copypaste from 'play' action) ==
    @room = params[:id][0,1]
    @current_players = CurrentPlayer.all(:conditions => {:room => @room})
    # ===
     return render(:action => 'play')
   end
    redirect_to_back
  end 

How can i avoid that code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:28:12+00:00Added an answer on May 14, 2026 at 4:28 am

    Just reuse your play action, since the parameters are the same. Also, I suggest the following to avoid so many nested if statements

    def play
      @room = params[:id][0,1]
      @current_players = CurrentPlayer.all(:conditions => {:room => @room})
    end
    
    def join
      @new_player = CurrentPlayer.new(:user_id => current_user.id, :team_id => nil, :room => params[:room], :mode => params[:mode], :ip => request.remote_ip)
      if ! @new_player.save
        play
        return render(:action => 'play')
      end
    
      @game = create_game(params[:room], params[:mode])
      if ! @game
        flash[:error] = 'Game not created'
        return(redirect_to_back)
      end
    
      flash[:notice] = 'Game created'
      return redirect_to(:action => "game_details", :recent => true)
    end 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know any library that will help doing that? I would write a
I have an ICollection that I know will only ever have one member. Currently,
I am trying in javascript to convert an integer (which I know will be
I know this will be a difficult question, so I am not necessarily looking
I know it will sound dumb or people will just say: read the documentations
Occasionally I will know that there is a .NET Framework function that returns a
I know this will let the user retrieve the latest 10 posts from cocacola
I know i will get this exception when i try to modify or remove
I would like to know how will performance go when I instantiate multiple classes
I have been trying to write a set of htaccess rules that will do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.