Home arrow Ruby-on-Rails arrow Logging Out, Events and Templates with Action Pack
RUBY-ON-RAILS

Logging Out, Events and Templates with Action Pack


In this thirteenth part of a multi-part series on the Action Pack library for Rails, you'll learn how to log out a user, clean up the events controller, and more. This article is excerpted from chapter six of the book Beginning Rails: From Novice to Professional, written by Jeffery Allan Hardy, Cloves Carneiro Jr. and Hampton Catlin (Apress; ISBN: 1590596862).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
June 27, 2011
TABLE OF CONTENTS:
  1. · Logging Out, Events and Templates with Action Pack
  2. · Improving the Events Controller and Templates

print this article
SEARCH DEVARTICLES

Logging Out, Events and Templates with Action Pack
(Page 1 of 2 )

Logging Out a User

The logout action is fairly straightforward. We clear out the :user_id session by setting its value to nil , and then we redirect back to the login action.

def logout

session[:user_id] = nil

redirect_to login_url end

This is a good way to clear out a specific session, but sometimes you want to clear the entire session without having to remember which keys to reset. Rails provides the method reset_session to do just that.

Weve done a lot of work on the users controller so far. Listing 6-14 shows what it should look like at this stage.

Listing 6-14. Actions Added in app/controllers/users_controller.rb

class UsersController < ApplicationController def show @user = User.find(params[:id]) end

def new @user = User.new end

def create @user = User.new(params[:user]) if @user.save

flash[:notice] = 'Thanks for signing up!' redirect_to :controller => 'events', :action => 'index' else render :action => 'new' end end

def edit @user = User.find(params[:id]) end

def update @user = User.find(params[:id]) if @user.update_attributes(params[:user])

flash[:notice] = 'Information updated' redirect_to :action => 'show', :id => @user.id else render :action => 'edit' end end

def login if request.post?

if user = User.authenticate(params[:login], params[:password]) session[:user_id] = user.id redirect_to :controller => 'events', :action => 'index'

else flash[:notice] = 'Invalid login/password combination' end end end

def logout

session[:user_id] = nil

redirect_to :action => 'login'

end end


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials