Error message on the redirected page

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 :slight_smile:
thanks in advance

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

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 :slight_smile:

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 :slight_smile:

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.

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