rails cancan访问被拒绝

时间:2014-02-04 02:22:13

标签: ruby-on-rails cancan

我已经将自定义身份验证与CanCan相结合,令我沮丧的是,它并没有按预期正常工作。所以,这是我的控制器中的代码片段

class Profile < ActiveRecord::Base
    belongs_to :user, :inverse_of => :profile
    delegate   :email, to: :user
    validates_presence_of :user_id
end

class Api::V1::ProfileController < Api::V1::UserActivityController

    load_and_authorize_resource :user
    load_and_authorize_resource :profile, :through => :user, :class => "Profile"

    def index
       @profile = subject_user.profile
       authorize! :index, @profile
    end
end

这是Ability

class Ability
   include CanCan::Ability

   def initialize(user)

       alias_action :index, :show, :to => :read 

       can :read, Profile
    end
end

如果我尝试can :read, :all它可以正常工作但can :read, Profile则失败。我有一个最初的假设,即Profile类中的delagate可能会导致拒绝,但事实并非如此。有什么想法吗?

该页面应该使用rabl

呈现为JSON
object @profile

attributes :first_name, :last_name, :gender

- 更新 -

我认为命名空间和嵌套会导致我的代码出现问题。我的控制实际上是命名空间(我更新了控制器代码),我试图通过以下路由访问Profile

namespace :user
    namespace :profile
end 

我在load_and_authorize_resource中尝试了ProfileController的以下代码(也更新了上面的控制器代码),但它无效。有关如何修改它的任何想法?是否需要在Ability类中更改任何内容?

load_and_authorize_resource :user
load_and_authorize_resource :profile, :through => :user, :class => "Profile"

0 个答案:

没有答案