使用 devise Rails 3.1 设置验证码

发布于 2024-12-05 19:08:49 字数 1673 浏览 0 评论 0原文

我按照 devise wiki 此处 进行操作当我尝试创建新用户时出现模板错误。这就是我所做的。

在 config/environment.rb 中设置我的公钥和私钥,

ENV['RECAPTCHA_PUBLIC_KEY']='examplepubkey'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'exampleprivkey'

然后将 gem 添加到我的 gemfile 并运行捆绑安装,

gem 'recaptcha', :require => 'recaptcha/rails'

然后通过运行 Rails g controller Registrations create 并将其添加到文件中来添加注册控制器。

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

我还将验证码标签添加到了views/devise/registrations/new

<div><%= recaptcha_tags %></div>

然后我编辑了路由文件,如下所示:

devise_for :users, :controllers => { :registrations => "registrations" }, :path => "users"

当我在浏览器中查看链接时,我得到

Template is missing

Missing template registrations/new with {:handlers=>[:erb, :builder, :coffee], 
:formats=>[:html], :locale=>[:en, :en]}. Searched in: * 
"/Users/thomascioppettini/rails_projects/want_freight/app/views" *
"/Users/thomascioppettini/.rvm/gems/ruby-1.9.2-p180@standard/gems/devise-1.4.5/app/views"

[编辑]

我能够通过删除验证码来显示验证码我添加了有关控制器的信息,但是当我在文本字段中添加任何文本或将其留空时,验证码会通过。

[编辑2] 我能够弄清楚如何解决该问题,并将在堆栈溢出允许时发布解决方案。

I followed the devise wiki here and I get a template error when I try to create a new user. This is what I have done.

Set up my public and private keys in config/environment.rb

ENV['RECAPTCHA_PUBLIC_KEY']='examplepubkey'
ENV['RECAPTCHA_PRIVATE_KEY'] = 'exampleprivkey'

I then added the gem to my gemfile and ran bundle install

gem 'recaptcha', :require => 'recaptcha/rails'

I then added a registrations controller by running rails g controller Registrations create and adding this to the file.

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 also added the recaptcha tags to views/devise/registrations/new

<div><%= recaptcha_tags %></div>

Then I edited the routes file to look like:

devise_for :users, :controllers => { :registrations => "registrations" }, :path => "users"

When I check out the link in my browser I get

Template is missing

Missing template registrations/new with {:handlers=>[:erb, :builder, :coffee], 
:formats=>[:html], :locale=>[:en, :en]}. Searched in: * 
"/Users/thomascioppettini/rails_projects/want_freight/app/views" *
"/Users/thomascioppettini/.rvm/gems/ruby-1.9.2-p180@standard/gems/devise-1.4.5/app/views"

[edit]

I was able to get the captcha to appear by deleting the bit I added about controllers, but the captcha passes when I add any text into the text field or leave it blank.

[edit2]
I was able to figure out how to solve the problem and will post the solution when stack overflow will allow me to.

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

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

发布评论

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

评论(1

谁的年少不轻狂 2024-12-12 19:08:49

我能够通过销毁我使用 Rails g 控制器注册创建并按照本网站建议手动添加文件设置的控制器来使验证码正常工作。 使用设备设置验证码

I was able to get the recaptcha to work by destroying the controller I had set up using rails g controller Registrations create and adding the files by hand as this site suggests. setting up recaptcha with devise

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文