Rails 3-插件返回“未定义的局部变量”

发布于 2024-10-05 19:09:04 字数 883 浏览 2 评论 0原文

我有一个自定义插件(我没有编写它),它不能在 Rails 3 上工作,但是它可以在 Rails 2 上工作。它用于自定义身份验证方案,主模块如下所示:

#lib/auth.rb
module ActionController

  module Verification
    module ClassMethods
      def verify_identity(options = {})
        class_eval(%(before_filter :validate_identity, :only => options[:only], :except => options[:except]))
      end
    end
  end

  class Base
    #some configuration variables in here

    def validate_identity
      #does stuff to validate the identity
    end
  end

end

#init.rb
require 'auth'
require 'auth_helper'
ActionView::Base.send(:include, AuthHelper)

AuthHelper 包含一个简单的基于组成员身份进行身份验证的辅助方法。

当我在操作控制器上包含“verify_identity”时:

class TestController < ApplicationController
  verify_identity
  ....
end

我收到路由错误:TestController:Class 的未定义局部变量或方法“verify_identity”。我有什么想法可以解决这个问题吗?谢谢!

I have a custom plugin (I didn't write it) that is not working on rails 3, however it did work with rails 2. It is for a custom authentication scheme, here is what the main module looks like:

#lib/auth.rb
module ActionController

  module Verification
    module ClassMethods
      def verify_identity(options = {})
        class_eval(%(before_filter :validate_identity, :only => options[:only], :except => options[:except]))
      end
    end
  end

  class Base
    #some configuration variables in here

    def validate_identity
      #does stuff to validate the identity
    end
  end

end

#init.rb
require 'auth'
require 'auth_helper'
ActionView::Base.send(:include, AuthHelper)

AuthHelper contains a simple helper method for authenticating, base on a group membership.

When I include 'verify_identity' on an actioncontroller:

class TestController < ApplicationController
  verify_identity
  ....
end

I get a routing error: undefined local variable or method `verify_identity' for TestController:Class. Any ideas how I can fix this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-10-12 19:09:04

它在 2.3 中有效,因为那里有一个 ActionController::Verification 模块。它在 3.0 中不起作用,因为该模块不存在。不要依赖 Rails 来拥有可以挂钩的模块,而是像这样定义自己的模块:

require 'active_support/concern'
module Your
  module Mod
    extend ActiveSupport::Concern
    module ClassMethods
      def verify_identity(options = {})
        # code goes here
      end
    end
  end
end

并使用:

ActionController::Base.send(:include, Your::Mod)

来使其功能可用。 ActiveSupport::Concern 支持您在模块内拥有 ClassMethodsInstanceMethods 模块,并且它负责将这些模块中的方法加载到正确的位置模块包含的任何区域。

It worked in 2.3 because there was an ActionController::Verification module back there. It's not working in 3.0 because this module doesn't exist. Rather than relying on Rails to have a module that you can hook into, define your own like this:

require 'active_support/concern'
module Your
  module Mod
    extend ActiveSupport::Concern
    module ClassMethods
      def verify_identity(options = {})
        # code goes here
      end
    end
  end
end

and use:

ActionController::Base.send(:include, Your::Mod)

To make its functions available. ActiveSupport::Concern supports you having a ClassMethods and InstanceMethods module inside your module and it takes care of loading the methods in these modules into the correct areas of whatever the module is included into.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文