我正在尝试配置声明式授权到我的rails博客应用程序。在按要求完成所有操作后,我做了rails s来启动服务器。我遇到了以下错误:
rails s
/var/lib/gems/1.8/gems/activerecord-3.2.9/lib/active_record/dynamic_matchers.rb:50:in `method_missing': undefined local variable or method `scopes' for ActiveRecord::Base:Class (NameError)
from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization/in_model.rb:37:in `included'
from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization/in_model.rb:36:in `module_eval'
from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization/in_model.rb:36:in `included'
from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization.rb:17:in `include'
from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization.rb:17:in `send'
from /var/lib/gems/1.8/gems/declarative_authorization-0.5.2/lib/declarative_authorization.rb:17
from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `require'
from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:68:in `require'
from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `each'
from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:66:in `require'
from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `each'
from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler/runtime.rb:55:in `require'
from /var/lib/gems/1.8/gems/bundler-1.2.3/lib/bundler.rb:128:in `require'
from /blogging/config/application.rb:7
from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:53:in `require'
from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:53
from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:50:in `tap'
from /var/lib/gems/1.8/gems/railties-3.2.9/lib/rails/commands.rb:50
from script/rails:6:in `require'
from script/rails:6
我的博客应用程序有点类似于这个博客应用程序。
请帮忙解决这个问题。谢谢。:) - - - - - -
编辑:在您关于使用authlogic的评论之后。下面是另一个有相同错误的github页面:
https://github.com/binarylogic/authlogic/issues/316将你的authlogic gem代码替换为:
gem "authlogic", :git => "git://github.com/binarylogic/authlogic.git"
这是与rails的兼容性错误。看看这个github页面的错误:
https://github.com/stffn/declarative_authorization/issues/102他们已经通过编辑lib/declarative_authorization/in_model修复了它。rb
def self.included(base) # :nodoc:
#base.extend(ClassMethods)
base.module_eval do
- scopes[:with_permissions_to] = lambda do |parent_scope, *args|
- options = args.last.is_a?(Hash) ? args.pop : {}
- privilege = (args[0] || :read).to_sym
- privileges = [privilege]
- context =
- if options[:context]
- options[:context]
- elsif parent_scope.respond_to?(:proxy_reflection)
- parent_scope.proxy_reflection.klass.name.tableize.to_sym
- elsif parent_scope.respond_to?(:decl_auth_context)
- parent_scope.decl_auth_context
- else
- parent_scope.name.tableize.to_sym
- end
-
- user = options[:user] || Authorization.current_user
+ if Rails.version < "3.1"
+ scopes[:with_permissions_to] = lambda do |parent_scope, *args|
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ privilege = (args[0] || :read).to_sym
+ privileges = [privilege]
+ context =
+ if options[:context]
+ options[:context]
+ elsif parent_scope.respond_to?(:proxy_reflection)
+ parent_scope.proxy_reflection.klass.name.tableize.to_sym
+ elsif parent_scope.respond_to?(:decl_auth_context)
+ parent_scope.decl_auth_context
+ else
+ parent_scope.name.tableize.to_sym
+ end
- engine = options[:engine] || Authorization::Engine.instance
- engine.permit!(privileges, :user => user, :skip_attribute_test => true,
- :context => context)
+ user = options[:user] || Authorization.current_user
+
+ engine = options[:engine] || Authorization::Engine.instance
+ engine.permit!(privileges, :user => user, :skip_attribute_test => true,
+ :context => context)
- obligation_scope_for( privileges, :user => user,
- :context => context, :engine => engine, :model => parent_scope)
+ obligation_scope_for( privileges, :user => user,
+ :context => context, :engine => engine, :model => parent_scope)
+ end
end
# Builds and returns a scope with joins and conditions satisfying all obligations.
@@ -96,7 +98,32 @@ def self.obligation_scope_for( privileges, options = {} )
# current user.
#
def self.with_permissions_to (*args)
- scopes[:with_permissions_to].call(self, *args)
+ if Rails.version < "3.1"
+ scopes[:with_permissions_to].call(self, *args)
+ else
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ privilege = (args[0] || :read).to_sym
+ privileges = [privilege]
+
+ parent_scope = scoped
+ context =
+ if options[:context]
+ options[:context]
+ elsif parent_scope.klass.respond_to?(:decl_auth_context)
+ parent_scope.klass.decl_auth_context
+ else
+ parent_scope.klass.name.tableize.to_sym
+ end
+
+ user = options[:user] || Authorization.current_user
+
+ engine = options[:engine] || Authorization::Engine.instance
+ engine.permit!(privileges, :user => user, :skip_attribute_test => true,
+ :context => context)
+
+ obligation_scope_for( privileges, :user => user,
+ :context => context, :engine => engine, :model => parent_scope.klass)
+ end
end
# Activates model security for the current model. Then, CRUD operations
和test/test_helper。rb文件:
map.connect ':controller/:action/:id'
end
else
- Rails::Application.routes.draw do
+ #Rails::Application.routes.draw do
+ Rails.application.routes.draw do
match '/name/spaced_things(/:action)' => 'name/spaced_things'
match '/deep/name_spaced/things(/:action)' => 'deep/name_spaced/things'
match '/:controller(/:action(/:id))'
@@ -146,7 +147,8 @@ def request! (user, action, reader, params = {})
unless Rails.version < "3"
def setup
- @routes = Rails::Application.routes
+ #@routes = Rails::Application.routes
+ @routes = Rails.application.routes
end
end
end