0

我正在尝试为我的rails博客应用程序配置声明授权。根据需要完成所有工作后,我开始使用rails来启动服务器。我遇到了以下错误。运行rails服务器时出错

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 

我的博客应用程序有点类似于这个blog应用程序。

请帮助解决此问题。谢谢。 :) -

+0

'method_missing':未定义的ActiveRecord :: Base:Class(NameError)的局部变量或方法'范围'这是错误。只要看看'范围'存储/位置在哪里? – 2013-05-02 10:24:29

+0

如果你能解释你的意思,那会很好。 – 2013-05-02 10:34:44

+0

你看过错误吗? – 2013-05-02 10:37:17

回答

2

编辑:关于使用authlogic的评论后。这里有一个具有相同错误的另一github上页:

https://github.com/binarylogic/authlogic/issues/316

取代你authlogic宝石的代码如下:

gem "authlogic", :git => "git://github.com/binarylogic/authlogic.git" 

这是一个轨道兼容性错误。看看这个页面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_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 
+0

我只在我的gemfile中使用了gem“authlogic”。这还不够吗? – 2013-05-02 11:45:01

+0

只是尝试我提到的 – 2013-05-02 11:47:33

+1

我的问题与您的答案解决。无需更改authlogic的路径。我从rubyforge应该是一个稳定的版本。我纠正了我的问题。现在另一个问题,未定义的方法'current_user' – 2013-05-02 12:18:57