1

I'd like to trigger an action from my rails controller when the user leaves a page. I fell onto the window.beforeunload event, but I don't know how I can call the function from my controller from the view. I have something like this:


 //function that checks if someone leaves the page
 //if so, send mail to admin with unconfirmed reservation
function goodbye(e) {
 //:action => do_something
}
window.onbeforeunload= goodbye;

Can someone help me? Thanks!

asked Dec 23, 2009 at 9:48

1 Answer 1

4

Looks like you need an AJAX request here. remote_function can be handy here. Something like:

function goodbye(e) {
 <%= remote_function :url => { :action => "do_something" } %>
}
answered Dec 23, 2009 at 10:42
Sign up to request clarification or add additional context in comments.

1 Comment

That does the trick indeed. But extra problem. When i go to another page, my method get's called, but i get an error because no according view exists. But all I want is that it does the action and then goes to the action which was clicked. Any solution to this?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.