0
0
Ruby on Railsframework~20 mins

Redirect and render in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redirect and Render Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when both redirect_to and render are called in a Rails controller action?

Consider this Rails controller action:

def example
  redirect_to root_path
  render :show
end

What will be the result when this action is called?

Ruby on Rails
def example
  redirect_to root_path
  render :show
end
AThe action redirects to root_path and then renders the show template, so both happen.
BRails ignores the render call and only performs the redirect_to.
CRails ignores the redirect_to and only renders the show template.
DRails raises a DoubleRenderError because you cannot call both redirect_to and render in the same action.
Attempts:
2 left
💡 Hint

Think about what Rails allows in a single controller action regarding response.

state_output
intermediate
1:30remaining
What is the HTTP status code when using redirect_to in Rails without specifying status?

In a Rails controller, if you call redirect_to some_path without specifying a status, what HTTP status code is sent to the browser?

Ruby on Rails
redirect_to some_path
A200 OK
B301 Moved Permanently
C302 Found (Temporary Redirect)
D404 Not Found
Attempts:
2 left
💡 Hint

Think about the default redirect status Rails uses for redirect_to.

📝 Syntax
advanced
2:00remaining
Which option correctly renders a JSON response and then redirects in a Rails controller?

Given the following controller code, which option will NOT cause an error?

def example
  # your code here
end
Ruby on Rails
def example
  # your code here
end
Aredirect_to root_path
B
render json: { message: 'Hello' }
redirect_to root_path
C
redirect_to root_path
render json: { message: 'Hello' }
Drender json: { message: 'Hello' }
Attempts:
2 left
💡 Hint

Remember Rails only allows one response per action.

🔧 Debug
advanced
2:30remaining
Why does this Rails controller action raise a DoubleRenderError?

Look at this controller action:

def show
  if params[:format] == 'json'
    render json: { message: 'Hello' }
  end
  redirect_to root_path
end

Why does it raise a DoubleRenderError?

Ruby on Rails
def show
  if params[:format] == 'json'
    render json: { message: 'Hello' }
  end
  redirect_to root_path
end
ABecause <code>redirect_to</code> is called after <code>render</code> without stopping execution.
BBecause <code>render</code> is missing a status code.
CBecause <code>redirect_to</code> cannot be used with JSON responses.
DBecause the <code>if</code> condition is always true.
Attempts:
2 left
💡 Hint

Think about what happens after render is called.

🧠 Conceptual
expert
3:00remaining
How does Rails decide what to do when both render and redirect_to are called conditionally in the same action?

Consider this controller action:

def index
  if params[:redirect]
    redirect_to root_path
  else
    render :index
  end
end

What will Rails do when params[:redirect] is true and when it is false?

Ruby on Rails
def index
  if params[:redirect]
    redirect_to root_path
  else
    render :index
  end
end
ARails always renders the index template regardless of the condition.
BWhen true, Rails redirects; when false, Rails renders the index template. No errors occur.
CRails raises a DoubleRenderError because both render and redirect_to exist in the action.
DRails ignores the redirect_to and always renders the index template.
Attempts:
2 left
💡 Hint

Think about conditional flow and response in Rails actions.