# Logging out feature

**URL:** <https://www.ruby-forum.com/t/logging-out-feature/185497>\
**Category:** Rails\
**Created:** [March 21, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/logging-out-feature/185497 "2010-03-21T16:59:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/logging-out-feature/185497/1 "2010-03-21T16:59:56Z")

</div>

Logging out feature,

Code:

def logout  
@title = “Login - Books”

```
session[:user_id] != nil
flash[:notice] = "You have sucessfully been logged out !"
redirect_to :controller => :books, :action => :index

```

end  
end

^^when this is run, it logs the user out (nill) and displays you have  
successfully been logged out,

Code v2:  
def logout  
@title = “Login - Books”

```
session[:user_id] != nil
flash[:notice] = "You have sucessfully been logged out

```

#{@user.screen\_name} !"  
redirect\_to :controller =\> :books, :action =\> :index  
end  
end

^^what i was hoping this would do is to display the users name as well,  
(this is another task), i understand the session but how can i bring the  
session back while the session is nill :S

thanks once again mates,

Jagmit G.

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 5:17pm UTC](https://www.ruby-forum.com/t/logging-out-feature/185497/2 "2010-03-21T17:17:54Z")

</div>

Quoting Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end  
> flash[:notice] = “You have sucessfully been logged out  
> #{@user.screen\_name} !”  
> redirect\_to :controller =\> :books, :action =\> :index  
> end  
> end
> 
> ^^what i was hoping this would do is to display the users name as well,  
> (this is another task), i understand the session but how can i bring the  
> session back while the session is nill :S

Where is the instance variable @user set? If it were my code, I’d do  
something like this:

def logout  
@title = “Login - Books”

name = User.find(session[:user\_id])  
session[:user\_id] != nil  
flash[:notice] = “You have sucessfully been logged out #{name}!”  
redirect\_to :controller =\> :books, :action =\> :index  
end

HTH,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/logging-out-feature/185497/3 "2010-03-21T17:34:39Z")

</div>

Jeffrey L. Taylor wrote:

> Quoting Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > end  
> > flash[:notice] = “You have sucessfully been logged out  
> > #{@user.screen\_name} !”  
> > redirect\_to :controller =\> :books, :action =\> :index  
> > end  
> > end
> > 
> > ^^what i was hoping this would do is to display the users name as well,  
> > (this is another task), i understand the session but how can i bring the  
> > session back while the session is nill :S
> 
> Where is the instance variable @user set? If it were my code, I’d do  
> something like this:
> 
> def logout  
> @title = “Login - Books”
> 
> name = User.find(session[:user\_id])  
> session[:user\_id] != nil  
> flash[:notice] = “You have sucessfully been logged out #{name}!”  
> redirect\_to :controller =\> :books, :action =\> :index  
> end
> 
> HTH,  
> Jeffrey

Good afternoon friend,

Your code doesnt seem to be working as it gives me a SyntaxError, at the  
end i added another end cause of the nill, it seems to work but the only  
thing which is displayed is “You have successfully been logged out #!”  
just that, no username.

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 6:08pm UTC](https://www.ruby-forum.com/t/logging-out-feature/185497/4 "2010-03-21T18:08:50Z")

</div>

On 21 March 2010 15:59, Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Logging out feature,
> 
> Code:
> 
> def logout  
> @title = “Login - Books”
> 
> session[:user\_id] != nil

Do you mean  
session[:user\_id] = nil

Colin

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 7:01pm UTC](https://www.ruby-forum.com/t/logging-out-feature/185497/5 "2010-03-21T19:01:31Z")

</div>

Thank you very much Jeffrey and Colin…

🙂

It worked perfectly and change the != to =

---

<div class="post-metadata">

**Author:** ![guru\_101](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/77aa72/32.png) [@guru\_101](https://www.ruby-forum.com/u/guru_101)\
**Post date:** [March 21, 2010, 6:02pm UTC](https://www.ruby-forum.com/t/logging-out-feature/185497/6 "2010-03-21T18:02:24Z")

</div>

Quoting Jagmit G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > (this is another task), i understand the session but how can i bring the  
> > > session[:user\_id] != nil  
> > > end i added another end cause of the nill, it seems to work but the only  
> > > thing which is displayed is “You have successfully been logged out #!”  
> > > just that, no username.

Oops, change:  
name = User.find(session[:user\_id])

To:  
name = User.find(session[:user\_id], :select =\>  
‘screen\_name’).screen\_name

Or maybe clearer:

def logout  
@title = “Login - Books”

```
user = User.find(session[:user_id], :select => 'screen_name')
session[:user_id] != nil
flash[:notice] = "You have sucessfully been logged out 

```

#{user.screen\_name}!"  
redirect\_to :controller =\> :books, :action =\> :index  
end

And yes, you need the additional end to close the class.

Jeffrey
