Acts_as_Authenticated Plugin
Unfortunately, Rails does not (surprisingly) contain any kind of built-in login system.
There are several plugins you can use–probably the best solution is Acts_as_authenticated.
To set up Acts_as_authenticated, you need to:
- Install the plugin:
script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated - Generate the user and account models:
script/generate authenticated user account - Rake the DB to add the users table:
rake db:migrate - Finally, add this line to application.rb controller so you can use the gem:
include AuthenticatedSystem
That’s it! To create a new account, hit up the signup page: http://localhost:3000/account/signup.
To protect pages so they require login, simply add this line to the beginning of your controller class:
before_filter :login_required
If you want to exclude certain views from requiring authentication, just add the list of exceptions:
before_filter :login_required, :except => [:list]
def list
# list view stuff ...
end
# ...
If you want to get information about the current user, you can tap into the current_user variable from any of your controllers. If the user is not logged in, it’s set to nil; otherwise, it’s an instance of User.
And that’s all! The user and account classes come with some pre-built code–signing up, logging in, deleting your own account, etc. All that functionality for free!
Tags: user management Posted in




Leave a Reply