2011-02-17 52 views
1

我想在我的应用程序中实现逻辑验证码。我脚手架简单的TextCaptcha存储问题和答案在数据库中。Rails 3 - 扩展ActionController

目前我有这个在初始化/ text_captcha.rb

require 'text_captcha' 
ActionController::Base.send(:include, TextCaptcha) 

这 “LIB/text_captcha.rb”:

module TextCaptcha 
    def self.included(base) 
     base.send(:include, InstanceMethods) 
    end 
    module InstanceMethods 
     def require_text_captcha 
     @captcha = "hello!" 
     end 
    end 
end 
在评论控制器

所以我有这个不得不@访问验证码在视图

before_filter :require_text_captcha 

坏的是我必须重新启动webrick每次我做出改变 - 所以我认为我以错误的方式做这件事?我可以摆脱初始值设定项,只需要“text_captcha”,我需要...或者有一种方法可以在“models/text_capctha.rb”中执行此操作,但我一开始可能会想到这一点。

回答

1

由于ApplicationController在Rails应用程序从ActionController::Base延伸,你可以这样做:

require 'text_captcha' 
class ApplicationController < ActionController::Base 
    include TextCaptcha 
end 

app/controllers/application_controller.rb

+0

我喜欢你现在发布。 – 2011-02-21 01:33:38