# Saving changes before the user leaves a form page?

**URL:** <https://www.ruby-forum.com/t/saving-changes-before-the-user-leaves-a-form-page/177810>\
**Category:** Rails\
**Created:** [October 24, 2009, 3:32am UTC](https://www.ruby-forum.com/t/saving-changes-before-the-user-leaves-a-form-page/177810 "2009-10-24T03:32:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [October 24, 2009, 3:32am UTC](https://www.ruby-forum.com/t/saving-changes-before-the-user-leaves-a-form-page/177810/1 "2009-10-24T03:32:05Z")

</div>

I’ve been handed a new design for an edit view with a form, that  
allows the user to move on to the next object in a list. This means  
that there could be pending edits that need to be saved. The design  
called for wants a prompt to see if the user wants to save or discard  
pending changes (ala Gmail).

What’s a good rails-y way to do this?

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [October 24, 2009, 4:27am UTC](https://www.ruby-forum.com/t/saving-changes-before-the-user-leaves-a-form-page/177810/2 "2009-10-24T04:27:06Z")

</div>

> I’ve been handed a new design for an edit view with a form, that  
> allows the user to move on to the next object in a list. This means  
> that there could be pending edits that need to be saved. The design  
> called for wants a prompt to see if the user wants to save or discard  
> pending changes (ala Gmail).
> 
> What’s a good rails-y way to do this?

Doubt Rails would get involved much as this has to happen client side  
(the user could go anywhere or click on anything right?)

I’d write some Javascript to loop through all the fields in the form  
and create another attribute “original-data” – using whatever the  
recommended naming scheme is… maybe it’s data-original, I don’t  
recall.

Set the current value to this attribute. Then add an onunload handler  
that checks to see if any changes have been made and if so warn the  
user.

-philip

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [October 24, 2009, 7:26am UTC](https://www.ruby-forum.com/t/saving-changes-before-the-user-leaves-a-form-page/177810/3 "2009-10-24T07:26:46Z")

</div>

hmmm… thx, Phillip…

I was thinking that there was a rails aspect in that there’d be  
something tricky in catching the onunload event in JS and then if the  
user wanted to save, posting back from there to my rails action to  
handle the saving… no?

I guess I’m not seeing how I post to that action from JS… maybe I’m  
overthinking it and it’s simpler than that… maybe I’m just tired 🙂

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [October 27, 2009, 7:08pm UTC](https://www.ruby-forum.com/t/saving-changes-before-the-user-leaves-a-form-page/177810/4 "2009-10-27T19:08:56Z")

</div>

that sounds nice and simple… thx Marnen

On Oct 24, 3:48 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![lunaclaire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@lunaclaire](https://www.ruby-forum.com/u/lunaclaire)\
**Post date:** [October 25, 2009, 12:48am UTC](https://www.ruby-forum.com/t/saving-changes-before-the-user-leaves-a-form-page/177810/5 "2009-10-25T00:48:21Z")

</div>

lunaclaire wrote:

> hmmm… thx, Phillip…
> 
> I was thinking that there was a rails aspect in that there’d be  
> something tricky in catching the onunload event in JS and then if the  
> user wanted to save, posting back from there to my rails action to  
> handle the saving… no?
> 
> I guess I’m not seeing how I post to that action from JS…

Have the JS call submit() on your form element. It’s still in the DOM  
with all the data that the user entered.

> maybe I’m  
> overthinking it and it’s simpler than that… maybe I’m just tired 🙂

🙂

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
