Forum: Ruby on Rails Redirecting user on object.save failures

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dc7c895399610211199703b7310aa85f?d=identicon&s=25 David Edwards (Guest)
on 2006-05-26 13:19
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?
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-05-26 15:04
(Received via mailing list)
On Friday, May 26, 2006, at 1:19 PM, David Edwards 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
>Rails@lists.rubyonrails.org
>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
This topic is locked and can not be replied to.