Use easy_captcha to generate a logon Verification Code Ruby on Rails

Source: Internet
Author: User


(1) Add gem to gemfile, then bundle install

gem ‘easy_captcha‘, git: ‘https://github.com/phatworx/easy_captcha.git‘gem ‘rmagick‘

(2) After bundle Execute

rails g easy_captcha:install

After running this sentence, we can see the changes in APP/controllers/application_controller.rb and config/routes. RB.

Class applicationcontroller <actioncontroller: Base
+ # Reset CAPTCHA code after each request for security
+ After_filter: reset_last_captcha_code!

 

Config/routes. RB:

CMS: application. routes. Draw do
+ Captcha_route

(3) modify the user login file APP/controllers/sessions_controller.rb

Def create if user = cmsuser. Authenticate (Params [: username], Params [: Password]) session [: user_id] = user. ID-redirect_to root_path + If captcha_valid? Params [: CAPTCHA] + redirect_to root_path + else + flash. now [: Error] = "Verification code error" + render: New + end else flash. now [: Error] = "Incorrect username or password" render: New

(4) modify the corresponding view, APP/views/sessions/new.html. ERB

<% = Form_tag login_path, method: Post, class: "form-signin" do %> <input type = "text" name = "username" class = "form-control" Placeholder = "email" required autofocus> <input type =" password "name =" password "class =" form-control "Placeholder =" password "required> + <p> enter the Verification Code <% = text_field_tag: CAPTCHA %> </P> + <p> <% = captcha_tag %> </P> <button class = "BTN-lg btn-Primary BTN-block" type =" submit "> logon </button> <% if FLASH [: error] %> <P class = "notice" style = "color: red;"> <% = FLASH [: error] %> </P> <% end %>

 

Use easy_captcha to generate a logon Verification Code Ruby on Rails

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.