使用 Recaptcha 设置 Devise - Rails 3
我尝试根据之前提出的有关堆栈溢出的问题进行设置,但未能使其正常工作。验证码显示在我的表单上,但用户仍然可以在不填写验证码的情况下注册。
我正在按照以下说明进行操作。 https://github.com/plataformatec/devise/ wiki/How-To:-Use-Recaptcha-with-Devise
- 我已经得到了我的私人和公共 recaptcha 密钥
Environment.rb
ENV['RECAPTCHA_PUBLIC_KEY'] = 'mykey1234567'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'mykey1234567'
- 我已经安装了 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'
- 我已将验证码标签添加到我的注册视图中。
new.html.erb
<%= recaptcha_tags %>
<p><%= f.submit "Sign up" %></p>
创建注册控制器
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
- I've got my private and public recaptcha keys
Environment.rb
ENV['RECAPTCHA_PUBLIC_KEY'] = 'mykey1234567'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'mykey1234567'
- 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'
- I've added recaptcha tags to my registration view.
new.html.erb
<%= recaptcha_tags %>
<p><%= f.submit "Sign up" %></p>
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还需要告诉 Devise 使用您自定义的 RegistrationsController。您可以通过在
devise_for
声明中指定:controllers
选项来完成此操作。如果没有这个,Devise::RegistrationsController
就会被调用,这可能解释了为什么 recaptcha 不起作用。You also need to tell Devise to use your customized RegistrationsController. You do this by specifying the
:controllers
options in yourdevise_for
declaration. Without this theDevise::RegistrationsController
is called, which probably explains why recaptcha isn't working.