Recaptcha仅在页面刷新后显示 - Rails 4 - Devise

时间:2013-11-19 06:13:58

标签: devise ruby-on-rails-4 recaptcha

我已经根据gem指令安装了recaptcha,但是当我查看sign_up页面(使用Devise)时,直到我刷新页面才会出现catcha。

通过其他评论,建议通过将sign_in链接更改为:

来禁用turbolink(我正在使用)
<%= link_to "Sign up", new_registration_path, "data-no-turbolink" => true %><br />

我试过这个,但在刷新页面之前我仍然没有得到验证码。

相关代码:

查看/设计/注册/ new.html.erb

................
<%= recaptcha_tags %>
................

/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    if !verify_recaptcha
     flash.delete :recaptcha_error
     build_resource
     resource.valid?
     resource.errors.add(:base, "There was an error with the recaptcha code below. Please re-enter the code.")
     clean_up_passwords(resource)
     respond_with_navigational(resource) { render_with_scope :new }
   else
    flash.delete :recaptcha_error
    super
   end
  end

  def clean_up_passwords(*args)
   # Delete or comment out this method to prevent the password fields from 
   # repopulating after a failed registration
  end
 end

2 个答案:

答案 0 :(得分:1)

问题是我需要在链接中包含“class:”:

<%= link_to "Sign up", new_user_registration_path, "data-no-turbolink" => true, class: "btn btn-primary btn-med" %>

答案 1 :(得分:0)

只需在控制器中尝试此代码

def create  
  if resource.valid? && !verify_recaptcha  
    clean_up_passwords(resource)  
    flash.delete :recaptcha_error   

  elsif !resource.valid? && verify_recaptcha  
    clean_up_passwords resource  
    respond_with resource  

  elsif !resource.valid? && !verify_recaptcha  
    flash.now[:alert] = "Recaptcha error"  
    flash.delete :recaptcha_error  
    clean_up_passwords resource  
    respond_with resource  

  end  
 end  

在你的Views / devise / registrations / new.html.erb

<%= recaptcha_tags  display: {theme:  'red', tabindex:  5}, ssl: false, noscript: false %>