Calling a rails function from javascript
I'd like to trigger an action from my rails controller when t开发者_JS百科he 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!
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" } %>
}
精彩评论