Redirecting from an update action to the referrer of the edit

Posted by Mark Westling on Stack Overflow See other posts from Stack Overflow or by Mark Westling
Published on 2010-03-13T05:08:02Z Indexed on 2010/03/13 5:15 UTC
Read the original article Hit count: 312

Filed under:

My Rails 2.3 application has a User model and the usual controller actions. The edit form can be reached two ways: when a user edits his own profile from the home page, or when an admin user edits someone else's profile from users collection.

What I'd like to do is have the update action redirect back to the referred of the edit action, not the update action. If I do a simple redirect_to(:back) within update, it goes back to the edit form -- not good.

One solution is to forget entirely about referrers and redirect based on the current_user and the updated user: if they're the same, go back to the home page, else go to the users collection page. This will break if I ever add a third path to the edit form. It's doubtful I'll ever do this but I'd prefer a solution that's not so brittle.

Another solution is to store the referrer of edit form in a hidden field and then redirect to this value from inside the update action. This doesn't feel quite right, though I can't explain why.

Are there any better approaches? Or, should I stop worrying and go with one of the two I've mentioned?

© Stack Overflow or respective owner

Related posts about ruby-on-rails