参考:https://github.com/galetahub/simple-captcha
1.安装simple_captcha插件
rails plugin install git://github.com/kares/simple_captcha.git
rails generate simple_captcha
rake db:migrate
2.配置,在config/initializers目录下添加simple_captcha.rb
SimpleCaptcha.backend = :rmagick # default is :RMagick,如果安装的是rmagick插件,就写":rmagick";或选用":quick_magick"
SimpleCaptcha.image_options = {
:image_color => 'white',
:image_size => '110x30',
:text_color => 'black',
:text_font => 'arial',
:text_size => 22
} # these are the defaults 设置验证码插件的风格和样式
3.在config目录下routes.rb文件中添加下面代码:
match ":controller(/:action)", :controller => /simple_captcha\/[^\/]+/
4.在app/controllers目录下,application_controller.rb文件中添加下面代码:
include SimpleCaptcha::ControllerValidation
5.应用
(1)简单的引用
界面上:<%= show_simple_captcha %>
controller层:
if simple_captcha_valid?
flash[:notice]="success"
else
flash[:notice]="error"
end
(2)与某个model相关引用
model层添加:
class User < ActiveRecord::Base
include SimpleCaptcha::ModelValidation
validates_captcha : o n => :create, :message => 'invalid captcha'
end
界面上代码: <%= show_simple_captcha(:object=>"user") %>
源文章地址:https://github.com/kares/simple_captcha