CanCan单例加载具有多元化的类名

时间:2013-02-12 05:40:54

标签: cancan

我目前遇到最新版CanCan的问题。似乎gem不会加载具有多个名称的单例资源。

class Preferences < ActiveRecord::Base
  self.table_name = 'preferences'
  belongs_to :user
end

class User < ActiveRecord::Base
  has_one :preferences
end

#controller
class PreferencesController < ApplicationController
  before_filter :authenticate_user! #from Devise

  load_and_authorize_resource :singleton => true,
                              :through => :current_user,
                              :through_association => :preferences,
                              :parent => false
end

#ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    can [:read, :update], Preferences, :user_id => user.id
  end
end

当我运行控制器规范时,我一直收到这样的错误:

Failures:

 1) PreferencesController GET #show should return http success
     Failure/Error: get :show
     NameError:
       uninitialized constant Preference
     # ./spec/controllers/preferences_controller_spec.rb:10:in `block (3 levels) in <top (required)>'

似乎无论我尝试的load_resource个参数的组合是什么,我都无法再次传递我的规范而不会像这样手动加载资源:

class PreferencesController < ApplicationController
  before_filter :authenticate_user!

  def show
    @preferences = current_user.preferences
    authorize! :read, @preferences
  end
end

我需要使用load_and_authorize_resource参数的神奇组合吗?我有相同的配置工作在另一个单一控制器,其中资源属于正常的Rails约定(即用户has_one:配置文件),所以我知道这在正常情况下已经有效。

0 个答案:

没有答案
相关问题