Skip to content
This repository has been archived by the owner on Jan 24, 2018. It is now read-only.

Authentication (clearance gem)

toxis edited this page Jan 10, 2012 · 2 revisions

Installation

Include the gem in Gemfile :

gem "clearance"

And run this command :

rails generate clearance:install

Usage

Authorize users for a controller action :

class MyController < ApplicationController
  before_filter :authorize
  [...]
end

Reference the current user (in controller, view or helper) :

current_user

Example :

current_user.posts

Determine if current user is signed in or out (in controller, view or helper)

signed_in?
signed_out?

Example :

<% if signed_in? %>
  <%= current_user.email %>
  <%= link_to "Sign out", sign_out_path, :method => :delete %>
<% else %>
  <%= link_to "Sign in", sign_in_path %>
<% end %>

Overriding the model

It's very useful to overriding the model for personalize our own model's user (and for example to have a controller for profile page per users).

class User < ActiveRecord::Base
  include Clearance::User
  [...]
end