[Rails] want to destroy session on browser close for logout

1,033 views
Skip to first unread message

Rahul Mehta

unread,
Apr 29, 2010, 8:01:52 AM4/29/10
to Ruby on Rails: Talk
hii

i have my ror app website want to do destroy session on browser
close ,

please help how can i destroy..

i have used javascript function on body unload

function bunload()
{
document.location.href="del.rb";
}
</script>
</head>
<body onunload="bunload()">

and i have putted the del.rb file in my public folder and in that i
have putted the following code

session[:user] = nil

but its not destroying the session..

please help..

thanks

rahul

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Niels Meersschaert

unread,
Apr 29, 2010, 8:15:53 AM4/29/10
to rubyonra...@googlegroups.com
Ruby files aren't evaluated when they are in the Public folder, their contents are just returned to the user. You would need to have it call a controller action to run code.

Rahul Mehta

unread,
Apr 29, 2010, 8:32:04 AM4/29/10
to rubyonra...@googlegroups.com
hiii

thank for reply.

can u help me how to run logout action of controller user on window.location.href..

thanks

rahul

Rahul Mehta

unread,
Apr 29, 2010, 8:33:15 AM4/29/10
to Ruby on Rails: Talk
hiii niels

thank for reply.

can u help me how to run logout action of controller user on
window.location.href..

thanks

rahul

On Apr 29, 5:15 pm, Niels Meersschaert <nmeersscha...@mac.com> wrote:
> Ruby files aren't evaluated when they are in the Public folder, their contents are just returned to the user.  You would need to have it call a controller action to run code.
>
> On Apr 29, 2010, at 8:01 AM, Rahul Mehta wrote:
>
>
>
> > hii
>
> > i have my ror app website want to do destroy session on browser
> > close ,
>
> > please help how can i destroy..
>
> > i have used javascript function on body unload
>
> >  function bunload()
> > {
> >   document.location.href="del.rb";
> > }
> > </script>
> > </head>
> > <body onunload="bunload()">
>
> > and i have putted the del.rb file in my public folder  and in that i
> > have putted the following code
>
> >  session[:user] = nil
>
> > but its not destroying the session..
>
> > please help..
>
> > thanks
>
> > rahul
>
> > --
> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> > To post to this group, send email to rubyonra...@googlegroups.com.
> > To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.

Rahul Mehta

unread,
Apr 30, 2010, 1:19:25 AM4/30/10
to Ruby on Rails: Talk
Assuming you have a named_route for your logout action, you could try

function bunload() {
document.location.href="<%= logout_path %>";
}

However, I would point out that unload isn't guaranteed to be called &
is even less likely to call & wait to load a new resource so that you
can kill the session. Additionally, it's usually a mark of spam sites
(they do alert popups), so if at all possible you should avoid
onunload.

In reality, if you are using Rails defaults, the session is all stored
on the client anyway, so there is no server load to have the session
naturally expire when the browser quits (not when a user opens a
different url or closes the window). If you are storing sessions on
the server, you can easily setup a sweeper to clear out sessions not
accessed in some time (or set that as a default in your environment)

Niels


instead of logout_path m giving string "/user/logout" which is the url
for logout but its not working why.

please help..

rahul


m trying to call my user controller logout action from onunload
function
Reply all
Reply to author
Forward
0 new messages