关于Ruby on Rails上validation码的建议

我想在Rails项目中实现一个表单提交的validation码,但我不知道该怎么做。 我倾向于简化实现,使用时的可靠性,因为它太复杂,因为我的应用程序不需要太高的安全级别。

有人有什么建议吗?

将CAPTCHA添加到Rails应用程序的最简单方法是使用Ambethia reCAPTCHA

1.安装:

config.gem "ambethia-recaptcha", :lib => "recaptcha/rails", :source => "http://gems.github.com" 

如果您愿意,也可以将其安装为插件。

2.获取reCAPTCHA帐户:

您必须创建一个reCAPTCHA密钥。 您可以在reCAPTCHA站点上执行此操作。

3.用法:

使用recaptcha_tags输出必要的HTML代码,然后使用verify_recaptchavalidation输入。

4.进一步阅读:

  • Ambethia reCAPTCHA
  • reCAPTCHA文档

安装

将以下内容添加到您的GEMFILE中

 gem "galetahub-simple_captcha", :require => "simple_captcha" 

要么

 gem 'galetahub-simple_captcha', :require => 'simple_captcha', :git => 'git://github.com/galetahub/simple-captcha.git' 

如果您正在使用Bundler(或使用包管理器进行Rails配置),请运行bundle install

建立

安装后,请按照以下简单步骤设置插件。 设置将取决于您的应用程序使用的rails版本。

 rails generate simple_captcha rake db:migrate 

用法

基于控制器

在“app / controllers / application.rb”文件中添加以下行

 ApplicationController < ActionController::Base include SimpleCaptcha::ControllerHelpers end 

在表单标签中的视图文件中添加此代码

 <%= show_simple_captcha %> 

并在控制器的操作中将其validation为

 if simple_captcha_valid? do this else do that end 

基于模型

在表单标签中的视图文件中添加此代码

 <%= show_simple_captcha(:object=>"user") %> 

并在模型类中添加此代码

 class User < ActiveRecord::Base apply_simple_captcha end 

FormBuilder助手

 <%= form_for @user do |form| -%> ... <%= form.simple_captcha :label => "Enter numbers.." %> ... <% end -%> 

使用validation码validation

注意:@ user.valid? 仍将按预期工作,它不会validationvalidation码。

 @user.valid_with_captcha? 

用validation码保存

注意:@ user.save仍然可以正常工作,它不会validationvalidation码。

 @user.save_with_captcha 

forms整合

SimpleCaptcha检测您是否使用Formtastic并附加

 “SimpleCaptcha::CustomFormBuilder”. <%= form.input :captcha, :as => :simple_captcha %> 

选项和示例

查看选项

 *label* - provides the custom text b/w the image and the text field, the default is “type the code from the image” *object* - the name of the object of the model class, to implement the model based captcha. *code_type* - return numeric only if set to 'numeric' 

全球选择

 image_style - provides the specific image style for the captcha image. 

这个插件有八种不同的风格可供选择......

  1. simply_blue
  2. simply_red
  3. simply_green
  4. 炭灰色
  5. embosed_silver
  6. 全黑
  7. distorted_black
  8. almost_invisible

默认样式是'simply_blue'。 您还可以指定“随机”以选择随机图像样式。

 distortion - handles the complexity of the image. The :distortion can be set to 'low', 'medium' or 'high'. Default is 'low'. 

*创建“rails_root / config / initializers / simple_captcha.rb”*

 SimpleCaptcha.setup do |sc| # default: 100x28 sc.image_size = '120x40' # default: 5 sc.length = 6 # default: simply_blue # possible values: # 'embosed_silver', # 'simply_red', # 'simply_green', # 'simply_blue', # 'distorted_black', # 'all_black', # 'charcoal_grey', # 'almost_invisible' # 'random' sc.image_style = 'simply_green' # default: low # possible values: 'low', 'medium', 'high', 'random' sc.distortion = 'medium' end 

您可以添加自己的风格:

 SimpleCaptcha.setup do |sc| sc.image_style = 'mycaptha' sc.add_image_style('mycaptha', [ "-background '#F4F7F8'", "-fill '#86818B'", "-border 1", "-bordercolor '#E0E2E3'"]) end 

您还可以提供安装image_magick的路径:

 SimpleCaptcha.setup do |sc| sc.image_magick_path = '/usr/bin' # you can check this from console by running: which convert end 

您可以提供应存储tmp文件的路径。 当你没有访问/ tmp(默认目录)时它很有用

 SimpleCaptcha.setup do |sc| sc.tmp_path = '/tmp' # or somewhere in project eg. Rails.root.join('tmp/simple_captcha').to_s, make shure directory exists end 

如何更改SimpleCaptcha DOM元素的CSS?

您可以根据需要在此文件中更改SimpleCaptcha DOM元素的CSS。

* /应用/视图/ simple_captcha / _simple_captcha.erb *

查看示例

控制器实例

 <%= show_simple_captcha %> <%= show_simple_captcha(:label => "human authentication") %> 

基于模型的例子

 <%= show_simple_captcha(:object => 'user', :label => "human authentication") %> 

型号选项

 message - provides the custom message on failure of captcha authentication the default is “Secret Code did not match with the Image” add_to_base - if set to true, appends the error message to the base. 

模型的例子

 class User < ActiveRecord::Base apply_simple_captcha end class User < ActiveRecord::Base apply_simple_captcha :message => "The secret Image and code were different", :add_to_base => true end 

那么,ReCaptcha将完成这项工作,并且有许多在线教程。

但是,您必须在控制器中编写一个正确的“def create”(创建方法),它将传递表单中的任何内容并同时validationRecaptcha。 然后它会很好地工作。

它有一个小问题。 将ReCaptcha插入表单后,表单validation停止工作。 但是,可以使用插入模型文件中的简单代码来修复它:

 after_validation :on => :create 

(:create =是控制器中的“def create”方法)。 它将强制表单首先validation表单,然后validationRecaptcha。

我在我的一个PHP项目中使用了Recaptcha。 http://recaptcha.net/
根据该网站,它还有Ruby插件( http://recaptcha.net/resources.html )。 虽然第一个ruby链接不起作用,但下一个链接仍然有效。 http://svn.ambethia.com/pub/rails/plugins/recaptcha/检查一下。

我已经使用ambethia rec来进行导轨应用。 它最容易比其他

就function而言,reCAPTCHA for rails非常棒。 但是,如果您需要XHTMLvalidation,请运行! 此插件不会(也可能永远不会)validation。 我发现令人尴尬的是,我的整个网站上只有一个页面无法validation – 它是带有reCAPTCHA的页面。 如果有其他选择,我会接受。

如果您正在使用validation的CAPTCHA,并且(几乎)像reCAPTCHA一样容易使用,请尝试我的SlideCAPTCHA。 (几天前写的,需要在实际使用中进行一些测试。)我将其部署过程基于reCAPTCHA插件,但您可以使用CSS设置它的样式。

但它确实需要Ruby / GD,所以如果你还没有GD,我不能保证GD易于安装和使用!

Interesting Posts