سؤال

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?

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top