ryanb/cancan — Issue #527: Is cancan compatible with Rails 3.1 & Sorcery(authentification gem)
Author: blawzoo
Created: 2011-12-08 00:09:49+00:00
State: open

Hello I have a big big problem with cancan.

1-I've a user model.
2- each user can only manage the data that he creates

Here's the code:

models/ability.rb

class Ability
include CanCan::Ability

def initialize(user)
user ||= User.new # guest user (not logged in)
if user
can :manage, Profile, :user_id => user.id
can :manage, Album, :user_id => user.id
end

end
end

controllers/albums_controllers.rb

# -*- encoding : utf-8 -*-
class AlbumsController < ApplicationController

# Authentification before accessing Albums
before_filter :require_login, :except => [:not_authenticated]
load_and_authorize_resource

def index
@albums = Album.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @albums }
end
end

def show
@album = Client.find(params[:id])
authorize! :show, @album

respond_to do |format|
format.html # show.html.erb
format.json { render json: @album }
end
end

def new
@album = Album.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @album }
end
end

def edit
@album = Album.find(params[:id])
authorize! :edit, @album
end

def create
@album = Album.new(params[:album])

respond_to do |format|
if @album.save
format.html { redirect_to @album, notice: 'album was successfully created.' }
format.json { render json: @album, status: :created, location: @album }
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end

def update
@album = Album.find(params[:id])
authorize! :update, @album

respond_to do |format|
if @album.update_attributes(params[:album])
format.html { redirect_to @album, notice: 'Album was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end

def destroy
@album = Album.find(params[:id])
@album.destroy

respond_to do |format|
format.html { redirect_to albums_url }
format.json { head :ok }
end
end
end

Is it a Rails 3.1 vs Cancan compatibility?
Is a Sorcery gem vs Cancan ?