我已经根据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
答案 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 %>