Louis93
Louis93

Reputation: 3923

How do I call a controller action on the user exiting the session?

I add a bunch of records to my table during a user`s session. Once the user exits the session (closes the window, goes to a different website etc.) I want to be able to call a function like so:

def clear_list
    Shoe.where(user_id: @user_id).destroy_all
end

I know that before_filter enables you to call a controller action as the page loads. What can I use to call an action on page close?

Upvotes: 0

Views: 151

Answers (2)

scaryguy
scaryguy

Reputation: 7950

"Destroying a session" and "closing a page" don't mean the same thing.

I guess you're looking for a before_destroy callback for your Session model.

See docs.

Upvotes: 1

Rodrigo Zurek
Rodrigo Zurek

Reputation: 4575

Maybe you can do something with jquery here is another question about it: javascript detect browser close tab/close browser, but im not sure you would be able to run code before the page gets closed, you are better off handling sessions and background process.

Upvotes: 1

Related Questions