天天看點

Rails captcha in ubuntu system

Use rails scale to init the captcha.

I use a gem called simple_captcha , before you use this gem , first you must install the imagemagick , for the save , tap the command below:

sudo apt-get install imagemagick
sudo apt-get install libmagickcore-dev
sudo apt-get install libmagickwand-dev
           

Then add a line in the rails dir's Gemfile:

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

Init the simple_captcha config:

SimpleCaptcha.setup do |captcha_config|
  captcha_config.tmp_path = Rails.root + 'tmp'
  captcha_config.image_style = 'simply_red'
end
           

If there is no error , the rest option will so simple .

Include the helper in the application_comtroller.rb

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

In the view file :

<p><%= @output %></p>

<form method="get" action="" >
  <%= show_simple_captcha(:label => 'enter',:placeholder=>'place')%>
<input type="submit" value="submit" />
</form>
           

In the controller action:

if simple_captcha_valid?
      @output = 'right'
    else
      @output = 'error'
    end
           

For more information , please read the README file in this site : https://github.com/galetahub/simple-captcha