sscirrus
sscirrus

Reputation: 56719

Simple CanCan problem

I have just started with CanCan and here's a sample of the code:

# Ability.rb
def initialize(user)    
  user ||= User.new      

  can :read, Link
end

# view.html.erb
<% if can? :read, @link %>
  ...
<% end %>

This is from the github repo for CanCan but this doesn't seem to work (it returns false and stops the ... code from running).

When I change the view to <% if can? :read, Link %>, it works. But, this is different to the CanCan readme. Do you know where I'm going wrong here?

Upvotes: 0

Views: 370

Answers (2)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

can? :read, Link

is the concept of a link... something to the akin of "Can you read all links?"

can :read, Link do |link|
  link.account.id == user.account_id
end

This checks to make sure you can read the specific link object in question

Upvotes: 0

David Sulc
David Sulc

Reputation: 25994

Check that

  1. You don't have anything granting or removing rights to Link models below the line you displayed
  2. @link is not nil and is a Link

Upvotes: 1

Related Questions