views:

132

answers:

2

Hey all

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  A: 

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" } %>
}
neutrino
A: 

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?

Ignace