LearnThatStack Ace your next interview
Ruby on Rails · question
Question 7 of 70

What is the difference between render and redirect_to?

beginner
← All Ruby on Rails questions
Re-explain
Visual ↓

render builds the response from a template within the current request, while redirect_to tells the browser to send a new request to another URL.

  • render :new shows the new template without running the new action. Instance variables from the current action, like @user with its errors, stay available to the view.
  • redirect_to @user sends a 302 response with the new URL. The browser then requests that page, so the target action runs from the start and the old instance variables are gone.
def create
  @user = User.new(user_params)
  if @user.save
    redirect_to @user  # New request to show action
  else
    render :new, status: :unprocessable_entity  # Renders new template with @user errors
  end
end

That gives the usual pattern for forms. After a successful save, redirect, so refreshing the page does not submit the form again. After a failed save, render the form again with status 422. Turbo, the JavaScript library new Rails apps include since Rails 7, only shows a form response that redirects or carries an error status.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
See it - interactive diagram
render vs redirect_to: one request or two
Browser
Rails app
Save fails: POST /users
render :new
422 + form with errors
Save succeeds: POST /users
302, Location: /users/1
GET /users/1
show runs from the start
200 + user page
1step

The create action runs. @user.save returns false, so @user now holds the errors.

1 / 8

render answers the same request, so @user keeps its errors. redirect_to makes the browser send a new request, so the show action starts without any of create's instance variables.

© LearnThatStack - diagrams may not be republished without permission.

Other questions have diagrams like this too. Sign in to ask for one where it's missing - it's free.

Create a free account
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Ruby on Rails cheatsheet.

← Back to all Ruby on Rails questions
Pro · $10/mo

61 of 70 Ruby on Rails answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime