(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