Redirecting user on object.save failures


#1

Hi,

I’m trying to create a chatboard (I call it my BoomBox, each post is
called a “boom”) which appears on every page of a weblog I’m building. I
have a scaffold-built controller called Boom, and the form to post to
the BoomBox is written into my default layout. So, the form can be
viewed from several of my other controllers, in most of their actions.
The form posts to Boom#create.

What I’m having trouble with is handling where the user is returned when
@boom.save fails (for example, failing to include a required field). The
scaffold’s default code renders the Boom#new page, which is nice because
I get all the errors displayed and stuff (validations defined in the
Boom model).

What I would like instead is for the user to be returned to whichever
controller and action the form was submitted from (in a transparent,
HTTP 303 fashion). I’ve tried using redirect_to :back instead, which
works, but the error information is not carried across and displayed as
it would be when I was using render.

What I’ve thought about doing instead is parsing the HTTP_REFERER
environment variable (falling back to the root directory), grabbing the
controller and action from that, and passing those to the render method,
but I can’t help but feel there’s a tidier way of doing what I want.

Any thoughts?


#2

On Friday, May 26, 2006, at 1:19 PM, David E. wrote:

@boom.save fails (for example, failing to include a required field). The
What I’ve thought about doing instead is parsing the HTTP_REFERER
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

A common trick here is to store the location you want to go back to in
the session and then redirect to that on completion.

_Kevin