# Respond\_with and redirects on error

**URL:** <https://www.ruby-forum.com/t/respond-with-and-redirects-on-error/186961>\
**Category:** Rails\
**Created:** [April 15, 2010, 12:05am UTC](https://www.ruby-forum.com/t/respond-with-and-redirects-on-error/186961 "2010-04-15T00:05:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 15, 2010, 12:05am UTC](https://www.ruby-forum.com/t/respond-with-and-redirects-on-error/186961/1 "2010-04-15T00:05:39Z")

</div>

Hello,

I’m using edge Rails and I have the following code in my users  
controller, with the respective test:

def update  
@user = User.find(params[:id])  
if @user.update\_attributes(params[:user])  
flash[:notice] = t :user\_successfully\_updated  
end  
respond\_with(@user)  
end

test “should not update user if modifications are invalid” do  
put :update, :id =\> users(:foo).id, :user =\> {:name =\> nil}  
assert !assigns(:user).valid?  
assert\_redirected\_to edit\_user\_url(users(:foo))  
end

The last assertion fails, saying the response is a 200 instead of a  
redirect. From the information I found online about reponds\_to, it  
seems that a redirection to the edit action should have been done,  
given that the object is invalid, but that isn’t the case in this  
test. Am I missing something obvious here?

Thanks in advance,  
Andre

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 15, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/respond-with-and-redirects-on-error/186961/2 "2010-04-15T17:34:25Z")

</div>

removed\_email\_address@domain.invalid wrote:

> The last assertion fails, saying the response is a 200 instead of a  
> redirect.

Standard rails behaviour is not to redirect if the update fails.

If you redirect on failure, the submitted modifications in the form gets  
over-written. So say you modify a blog altering a large paragraph of  
your master work. However, you also accidentally delete the blog title  
and that causes a validation failure. What you want then is for the  
rendered form to contain the data you submitted, not the content as it  
is in the database. Redirecting to edit will pull the data from the  
database again and populate the form with that, thereby losing the  
changes the user is trying to submit.

Also you’ll get a fresh object - one without the error data. So you  
won’t have anything to populate the error report with. What needs to be  
passed back to the form, is the same object that failed to save.

That’s one of the down sides of separating the edit and update methods -  
because you end up with both having to be able to handle the form  
rendering.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 16, 2010, 1:39pm UTC](https://www.ruby-forum.com/t/respond-with-and-redirects-on-error/186961/3 "2010-04-16T13:39:05Z")

</div>

On Apr 15, 12:34 pm, Rob N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > The last assertion fails, saying the response is a 200 instead of a  
> > redirect.
> 
> Standard rails behaviour is not to redirect if the update fails.  
> […]

Got it, thanks!
