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!
1 Answer 1
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
alex.zherdev
24.2k8 gold badges65 silver badges56 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Ignace
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?
lang-js