Rails 3-插件返回“未定义的局部变量”
我有一个自定义插件(我没有编写它),它不能在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它在 2.3 中有效,因为那里有一个
ActionController::Verification
模块。它在 3.0 中不起作用,因为该模块不存在。不要依赖 Rails 来拥有可以挂钩的模块,而是像这样定义自己的模块:并使用:
ActionController::Base.send(:include, Your::Mod)
来使其功能可用。
ActiveSupport::Concern
支持您在模块内拥有ClassMethods
和InstanceMethods
模块,并且它负责将这些模块中的方法加载到正确的位置模块包含的任何区域。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:and use:
ActionController::Base.send(:include, Your::Mod)
To make its functions available.
ActiveSupport::Concern
supports you having aClassMethods
andInstanceMethods
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.