ryanb / cancan

Authorization Gem for Ruby on Rails.
MIT License
6.26k stars 782 forks source link

CanCan 2.0 - PostgreSQL - Namespace problem #960

Open phlegx opened 11 years ago

phlegx commented 11 years ago

Hi!

I have this routes configuration and my ability don't work. It redirects to the root path. Why?

...
  constraints subdomain: /^(?!www|secure|test|local)(\w+)/ do
    namespace :channels, path: '' do
      resource :profile
    ...

app/models/ability.rb

...
      can :access, Channels::ProfilesController
...

The application controller has the _enableauthorization. And the profiles controller inherits from the base controller and the base controller from the application controller.

class Channels::ProfilesController < Channels::BaseController
...
  def resource
    @profile ||= channel
  end
...

EDIT

Here an error using the console:

> Channel.accessible_by(ability)

CanCan::Error: The accessible_by call cannot be used with a block 'can' definition. The SQL cannot be determined for :index :channels

EDIT 2

If I write only this ability:

app/models/ability.rb

...
      can :access, Channel
...

and test it with ability.can? i get:

2.0.0p247 :026 > ability.can?(:access, Channel)
 => false
2.0.0p247 :026 > Channel.accessible_by(ability)
  Channel Load (1.2ms)  SELECT "channels".* FROM "channels" WHERE ('t'='f')

What ist this WHERE ('t'='f')?

8bithero commented 10 years ago

Having a similar problem. Have you found a solution for this?

graywh commented 10 years ago

If you're using CanCan 2.0, you need to use the right syntax, using symbols. https://github.com/ryanb/cancan/wiki/CanCan-2.0#wiki-defining-abilities