I've been going through the railscast on using the cancan gem but am stuck on how to only allow a user to visit their own show page.
My code looks like this:
Ability model
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.role == "admin"
can :manage, :all
else
can :read, :all
if user.role == "author"
can :create, Review
can :update, Review do |review|
review.try(:user) == user
end
can :update, User do |user|
user.try(:current_user) == current_user
end
end
if user.role == "owner"
can :update, Venue
end
end
end
end
User controller
class UsersController < ApplicationController
load_and_authorize_resource
end
A user (author) can only update their own reviews, but can currently view all users show pages by altering the URL.
What am I missing here?
can :read, :all
. You allowed viewing all pages to all users. – Ofilia