Rails 3-插件返回'undefined local variable'

时间:2010-11-29 19:16:44

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-plugins

我有一个自定义插件(我没有写它),它不能在rails 3上运行,但是它确实适用于rails 2.它是一个自定义身份验证方案,这是主模块的样子: / p>

#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包含一个基于组成员资格进行身份验证的简单帮助方法。

当我在actioncontroller上包含'verify_identity'时:

class TestController < ApplicationController
  verify_identity
  ....
end

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

1 个答案:

答案 0 :(得分:3)

它在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模块,它负责将这些模块中的方法加载到包含模块的正确区域。