使用 Recaptcha 设置 Devise - Rails 3

发布于 2024-11-09 09:03:17 字数 1629 浏览 7 评论 0原文

我尝试根据之前提出的有关堆栈溢出的问题进行设置,但未能使其正常工作。验证码显示在我的表单上,但用户仍然可以在不填写验证码的情况下注册。

我正在按照以下说明进行操作。 https://github.com/plataformatec/devise/ wiki/How-To:-Use-Recaptcha-with-Devise

  1. 我已经得到了我的私人和公共 recaptcha 密钥

Environment.rb

ENV['RECAPTCHA_PUBLIC_KEY']  = 'mykey1234567'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'mykey1234567'
  1. 我已经安装了 recaptcha gem。

Gemfile

source 'http://rubygems.org'

gem 'rails', '3.0.3'

gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'devise', '1.1.7'
gem "jquery-rails"
gem 'recaptcha', :require => 'recaptcha/rails'
  1. 我已将验证码标签添加到我的注册视图中。

new.html.erb

      <%= recaptcha_tags %>

  <p><%= f.submit "Sign up" %></p>
  1. 创建注册控制器

    rails 生成控制器注册创建

注册控制器

class RegistrationsController < Devise::RegistrationsController

def create
      if verify_recaptcha
        super
      else
        build_resource
        clean_up_passwords(resource)
        flash[:alert] = "There was an error with the recaptcha code below. Please re-enter the code and click submit."
        render_with_scope :new
      end
    end
end

我应该编辑我的路由文件,但不确定到底是什么原因导致了问题。

我的路线文件

  devise_for :troopers, :path => "troopers", :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

感谢您的帮助。

I have tried setting this up based on previously asked questionshere on stack overflow but haven't been able to get it working. The captcha is showing up on my forms but a user can still register without filling in the recaptcha.

I'm working with the following instructions.
https://github.com/plataformatec/devise/wiki/How-To:-Use-Recaptcha-with-Devise

  1. I've got my private and public recaptcha keys

Environment.rb

ENV['RECAPTCHA_PUBLIC_KEY']  = 'mykey1234567'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'mykey1234567'
  1. I've installed the recaptcha gem.

Gemfile

source 'http://rubygems.org'

gem 'rails', '3.0.3'

gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'devise', '1.1.7'
gem "jquery-rails"
gem 'recaptcha', :require => 'recaptcha/rails'
  1. I've added recaptcha tags to my registration view.

new.html.erb

      <%= recaptcha_tags %>

  <p><%= f.submit "Sign up" %></p>
  1. Created Registrations Controller

    rails generate controller Registrations create

Registrations Controller

class RegistrationsController < Devise::RegistrationsController

def create
      if verify_recaptcha
        super
      else
        build_resource
        clean_up_passwords(resource)
        flash[:alert] = "There was an error with the recaptcha code below. Please re-enter the code and click submit."
        render_with_scope :new
      end
    end
end

I am supposed to edit my routes file but not sure what exactly which may be the cause of the issue.

My Routes File

  devise_for :troopers, :path => "troopers", :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙幽之幻 2024-11-16 09:03:17

您还需要告诉 Devise 使用您自定义的 RegistrationsController。您可以通过在 devise_for 声明中指定 :controllers 选项来完成此操作。如果没有这个,Devise::RegistrationsController 就会被调用,这可能解释了为什么 recaptcha 不起作用。

  devise_for :troopers,
             :controllers => { :registrations => "registrations" },
             :path => "troopers",
             :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }

You also need to tell Devise to use your customized RegistrationsController. You do this by specifying the :controllers options in your devise_for declaration. Without this the Devise::RegistrationsController is called, which probably explains why recaptcha isn't working.

  devise_for :troopers,
             :controllers => { :registrations => "registrations" },
             :path => "troopers",
             :path_names => { :sign_in => "login", :sign_out => "logout", :sign_up => "register" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文