# Error message on the redirected page

**URL:** <https://www.ruby-forum.com/t/error-message-on-the-redirected-page/71496>\
**Category:** Rails\
**Created:** [October 9, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/error-message-on-the-redirected-page/71496 "2006-10-09T13:39:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 9, 2006, 1:39pm UTC](https://www.ruby-forum.com/t/error-message-on-the-redirected-page/71496/1 "2006-10-09T13:39:18Z")

</div>

def edit  
@product = Product.find(params[:id])  
end

def update  
@product = Product.find(params[:id])  
if @product.update\_attributes(params[:product])  
flash[:notice] = ‘Product was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @product  
else  
render :action =\> ‘edit’  
end  
end

when errors happen, to avoid showing update in the address bar, i have  
to redirect to edit. but the problem is the error message is lost. any  
suggestion/workaround so that i can catch and display the error message  
on the redirected page? this should be a basic problem for rails 🙂  
thanks in advance

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 9, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/error-message-on-the-redirected-page/71496/2 "2006-10-09T14:31:28Z")

</div>

use flash.keep to retain flash messages across multiple requests.  
stick that right before your redirect.

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 9, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/error-message-on-the-redirected-page/71496/3 "2006-10-09T15:03:48Z")

</div>

Jason N. wrote:

> use flash.keep to retain flash messages across multiple requests.  
> stick that right before your redirect.

ah, forget to mention, the flashes works fine. the problem is within the  
error\_messages\_for() and the error\_message\_on() scope. thanks for the  
answer 🙂

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 9, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/error-message-on-the-redirected-page/71496/4 "2006-10-09T23:20:17Z")

</div>

Jason N. wrote:

> Can you do something like
> 
> def edit  
> if request.post?  
> if update  
> flash and redirect to show  
> end  
> end  
> end
> 
> Then put update under protected and have it return false if there’s an  
> error. That way there’s no redirect when you have an error.
> 
> I’m pretty noob-ish at rails, but I like trying to respond to posts to  
> help me learn. So if this isn’t what you want, I apologize.
> 
> Jason

that’s very nice, no need to apologize please, i’m pretty new in rails  
too, about 3 weeks 🙂

that can do, but no, i don’t think i would do that, because it will  
constraint what post-action-methods (like update, create, destroy)  
should be in the page-action-methods (like edit, new, show), so it won’t  
be very flexible.

i don’t think this is the DRY solution, and don’t you think it’s kinda  
hard to maintain later, since everytime we add/remove a  
post-action-method, we have to edit the page-action-methods as well.

thanks in advance

Adrian L.

---

<div class="post-metadata">

**Author:** ![adrianliem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@adrianliem](https://www.ruby-forum.com/u/adrianliem)\
**Post date:** [October 9, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/error-message-on-the-redirected-page/71496/5 "2006-10-09T18:54:12Z")

</div>

Can you do something like

def edit  
if request.post?  
if update  
flash and redirect to show  
end  
end  
end

Then put update under protected and have it return false if there’s an  
error. That way there’s no redirect when you have an error.

I’m pretty noob-ish at rails, but I like trying to respond to posts to  
help me learn. So if this isn’t what you want, I apologize.

Jason
