Rails does not know about the state of the form until it is submitted, but you can add a pinch of javascript to help in this case.
$('#header > a').click(function(){ $('input').each(function(i){ if ( $(this).attr(value) != '' ) { if ( confirm('are you sure you want to leave this form unfinished?') != 'true ) { alert('quitter!); } } }); });
edit: Okay, so that only handles clicking the title link (as you indicated in your question), here is a solution that uses onbeforeunload. A full-page source, because I checked it to make sure that I give you something to rely on:
<html> <head> <title>Foo</title> <script> window.onbeforeunload = function(){ var inputs = document.getElementsByTagName('input'); var unfinished = 'false'; for (var i=0; i<inputs.length; ++i) { if ( inputs[i].value != '' ) { unfinished = 'true'; } } if ( unfinished == 'true' ) { return 'Are you sure you want to leave?'; } } </script> </head> <body> <form> <input name='foo'/> <input type='submit'/> </form> <a href='/'>Leave</a> </body> </html>
Of course, this is not a Rails-specific helper, but you could write your helper to spit out a script tag with something like this in it in your views. You probably also want to enter validation on specific forms, rather than just checking each entry for emptiness. Every shape I have ever made was a beautiful and unique snowflake in one form or another.
Unixmonkey
source share