# Help with 'new' form code - 'render' vs 'redirect\_to'

**URL:** <https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898>\
**Category:** rubyonrails-talk\
**Created:** [May 10, 2011, 8:23pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898 "2011-05-10T20:23:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigo\_Ruiz](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@Rodrigo\_Ruiz](https://discuss.rubyonrails.org/u/Rodrigo_Ruiz)\
**Post date:** [May 10, 2011, 8:23pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/1 "2011-05-10T20:23:13Z")

</div>

Hi, I’m having some trouble to understand the following code:

def new

@user = User.new

@title = “Sign up”

end

def create

@user = User.new(params[:user])

if @user.save

flash[:success] = “Welcome to the Sample App!”

redirect\_to @user

else

@title = “Sign up”

@user.password = “”

@user.password\_confirmation = “”

render ‘new’

end

end

Since I do ‘@user = User.new’ in the ‘new’ method, why do i need to do it again ‘@user = User.new(params[:user])’ instead of ‘@user.update\_attributes(params[:user])’ in the ‘create’ method?

Also I don’t get the difference between ‘render’ and ‘redirect\_to’, what I read was that ‘render’ doesn’t clean out the variables, whereas ‘redirect\_to’ does, so back to the first question…

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [May 10, 2011, 8:32pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/2 "2011-05-10T20:32:42Z")

</div>

> Hi, I'm having some trouble to understand the following code: def new @user = User.new @title = "Sign up" end
> 
> def create @user = User.new(params[:user]) if @user.save flash[:success] = "Welcome to the Sample App!" redirect\_to @user else @title = "Sign up" @user.password = "" @user.password\_confirmation = "" render 'new' end end Since I do '@user = User.new' in the 'new' method, why do i need to do it again '@user = User.new(params[:user])' instead of '@user.update\_attributes(params[:user])' in the 'create' method?

Each action in the controller is a new request to the web server so the create action has no knowledge of @user set up in the new action. It can only go by what is in params.

> Also I don't get the difference between 'render' and 'redirect\_to', what I read was that 'render' doesn't clean out the variables, whereas 'redirect\_to' does, so back to the first question...

Render will re-show the 'new' page, with the form for filling in the new user so the user can fix the errors @user still has the values from the data was submitted last time so the form will show the same data again. The redirect causes the 'show' \*action\* to be run for the user, which results in that user being shown.

Colin

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [May 10, 2011, 8:36pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/3 "2011-05-10T20:36:24Z")

</div>

> Hi, I'm having some trouble to understand the following code:
> 
> def new &nbsp;&nbsp;&nbsp;&nbsp;@user = User.new &nbsp;&nbsp;&nbsp;&nbsp;@title = "Sign up" &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@user = User.new(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;if @user.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:success] = "Welcome to the Sample App!" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @user &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@title = "Sign up" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.password = "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.password\_confirmation = "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render 'new' &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;Since I do '@user = User.new' in the 'new' method, why do i need to do it again '@user = User.new(params[:user])' instead of '@user.update\_attributes(params[:user])' in the 'create' method?

Because of the stateless nature of the HTTP protocol. The first request is for a form. The second request is a post of the filled form. In between those two, there could be moments or hours or days, and the server has long since lost interest in that object you create.

In the New method, you use that User.new to spin up the form inputs, to set your defaults, and that's about all.

In the Create method, you create a completely different empty user, fill it with the result of the POST, validate those inputs, and save if it passes. It's not really an update at all.

Walter

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [May 10, 2011, 9:17pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/4 "2011-05-10T21:17:17Z")

</div>

> > Hi, I'm having some trouble to understand the following code: def new &nbsp;&nbsp;&nbsp;&nbsp;@user = User.new &nbsp;&nbsp;&nbsp;&nbsp;@title = "Sign up" &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def create &nbsp;&nbsp;&nbsp;&nbsp;@user = User.new(params[:user]) &nbsp;&nbsp;&nbsp;&nbsp;if @user.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:success] = "Welcome to the Sample App!" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to @user &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@title = "Sign up" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.password = "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@user.password\_confirmation = "" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render 'new' &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;Since I do '@user = User.new' in the 'new' method, why do i need to do it again '@user = User.new(params[:user])' instead of '@user.update\_attributes(params[:user])' in the 'create' method?
> 
> Each action in the controller is a new request to the web server so the create action has no knowledge of @user set up in the new action. It can only go by what is in params.
> 
> > &nbsp;&nbsp;&nbsp;Also I don't get the difference between 'render' and 'redirect\_to', what I read was that 'render' doesn't clean out the variables, whereas 'redirect\_to' does, so back to the first question...
> 
> Render will re-show the 'new' page, with the form for filling in the new user so the user can fix the errors @user still has the values from the data was submitted last time so the form will show the same data again. The redirect causes the 'show' \*action\* to be run for the user, which results in that user being shown.

To be complete explicit, calling render just tells rails which template to use, whereas redirect\_to tells the browser that they should make a separate http request to the specified location

Fred

---

<div class="post-metadata">

**Author:** ![Rodrigo\_Ruiz](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@Rodrigo\_Ruiz](https://discuss.rubyonrails.org/u/Rodrigo_Ruiz)\
**Post date:** [May 16, 2011, 5:46pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/5 "2011-05-16T17:46:35Z")

</div>

if it has no knowledge of @user in the ‘new’ action, why do I need to do @user = User.new? What is the point, if it will be lost for other actions?

I thought that all that i wrote with ‘@’ in ruby was an instance variable, meaning it is accessible to the whole instance, meaning any of its methods.

---

<div class="post-metadata">

**Author:** ![DK11](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@DK11](https://discuss.rubyonrails.org/u/DK11)\
**Post date:** [May 16, 2011, 5:51pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/6 "2011-05-16T17:51:25Z")

</div>

> if it has no knowledge of @user in the ‘new’ action, why do I need to do @user = User.new? What is the point, if it will be lost for other actions?

@user is used in @user = User.new so that you have your new user object available for the form builder on the view.

> I thought that all that i wrote with ‘@’ in ruby was an instance variable, meaning it is accessible to the whole instance, meaning any of its methods.

It is but as others have mentioned before, and maybe in other words: whatever you create only applies to the current request. The next time the browser hits the controller, it is fresh and you start over (and maybe use params or sessions to keep the state as needed).

---

<div class="post-metadata">

**Author:** ![Rodrigo\_Ruiz](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@Rodrigo\_Ruiz](https://discuss.rubyonrails.org/u/Rodrigo_Ruiz)\
**Post date:** [May 16, 2011, 6:04pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/7 "2011-05-16T18:04:58Z")

</div>

Now I think I got it, thank you.

I need @user = User.new for the line ‘form\_for(@user)’ in the view ‘new.html.erb’ right?

And what is the point of making ‘@user’ instead of ‘user’ (local variable) if it will be lost anyway for other requests?

---

<div class="post-metadata">

**Author:** ![DK11](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@DK11](https://discuss.rubyonrails.org/u/DK11)\
**Post date:** [May 16, 2011, 6:22pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/8 "2011-05-16T18:22:50Z")

</div>

> Now I think I got it, thank you.

> I need @user = User.new for the line ‘form\_for(@user)’ in the view ‘new.html.erb’ right?

> 

Correct

> And what is the point of making ‘@user’ instead of ‘user’ (local variable) if it will be lost anyway for other requests?

Try it and you will understand… if you make it ‘user’ the form wont see it. Remember, ‘user’ inside a controller method is local to that method. If you have any class and want to make a variable visible for the whole class, not just the immediate method, you need to use @var (or another method). Think of the view as being in the same class as the controller but not inside the controller method.

---

<div class="post-metadata">

**Author:** ![Rodrigo\_Ruiz](https://avatars.discourse-cdn.com/v4/letter/r/7cd45c/32.png) [@Rodrigo\_Ruiz](https://discuss.rubyonrails.org/u/Rodrigo_Ruiz)\
**Post date:** [May 16, 2011, 7:42pm UTC](https://discuss.rubyonrails.org/t/help-with-new-form-code-render-vs-redirect-to/58898/9 "2011-05-16T19:42:06Z")

</div>

Thank you.
