Rails7+Devise+Captcha:#<Devise::RegistrationsController:0x00000000036600>

问题描述 投票:0回答:1

先决条件:

  • 轨道 7.0.4
  • Devise 4.9.0 支持热线/turbo
  • 响应者 3.1.0

Devise 具有默认配置

  config.responder.error_status = :unprocessable_entity
  config.responder.redirect_status = :see_other

里面有几行

它适用于标准设计/注册/新视图和处理。

但是当我尝试根据示例添加验证码检查时,它失败了(见下文)。

class ApplicationController < ActionController::Base

  before_action :configure_permitted_parameters, if: :devise_controller?

  # Check captcha on :create only
  prepend_before_action :check_captcha_on_sign_up, if: Proc.new { request.controller_class == Devise::RegistrationsController }, only: [:create]

  protected

  def check_captcha_on_sign_up
    if CaptchaJob.new.valid?(params[:"smart-token"], request.remote_ip)
      flash.delete :alert
    else
      build_resource(sign_up_params)
      resource.valid?
      clean_up_passwords(resource)
      flash.alert = "Please, confirm you're not a robot"
      respond_with_navigational(resource) do
        render :new
      end
    end
  end

end

如果我提交带有验证码的注册表单,一切都可以。

但是如果我在没有检查验证码的情况下提交表单,我会得到这个错误:

undefined method `users_url' for #<Devise::RegistrationsController:0x00000000036600>

...

      clean_up_passwords(resource)
      flash.alert = "Please, confirm you're not a robot"
>>>>> respond_with_navigational(resource) do
        render :new
      end
    end

异常原因部分包含消息:

ActionView::MissingTemplate: Missing template devise/registrations/new, devise/new, application/new with {:locale=>[:ru], :formats=>[:turbo_stream], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. 

但是文件

devise/registrations/new.html.erb
存在。

devise.rb 无评论:

Devise.setup do |config|
  config.mailer_sender = '***'
  require 'devise/orm/active_record'
  config.case_insensitive_keys = [:email]
  config.strip_whitespace_keys = [:email]
  config.skip_session_storage = [:http_auth]
  config.stretches = Rails.env.test? ? 1 : 12
  config.reconfirmable = true
  config.expire_all_remember_me_on_sign_out = true
  config.password_length = 6..128
  config.email_regexp = /\A[^@\s]+@[^@\s]+\z/
  config.reset_password_within = 6.hours
  config.sign_out_via = :delete
  config.responder.error_status = :unprocessable_entity
  config.responder.redirect_status = :see_other
end

当然,我尝试将

config.navigational_formats = ['*/*', :html, :turbo_stream]
添加到devise.rb,但它是针对以前的Devise版本的,无论如何都不起作用。

如果验证码无效,如何修复

check_captcha_on_sign_up
方法以使其正确呈现
new.html.erb
视图?

谢谢。

UPD:我创建了一个带有失败演示的新项目:https://github.com/noff/devise_rails7_captcha_fail_demo

ruby-on-rails devise hotwire-rails turbo-rails
1个回答
0
投票

在您的代码中,您要求

ApplicationController
渲染
:new
,如 2.1 - 渲染动作视图 Rails 中的布局和渲染 ,它会自动渲染与控制器关联的视图。您的代码发生在 ApplicationController 中,我认为它会尝试从 ApplicationController 重新呈现
new
视图。

所以你需要将你的用户发送到设计控制器生成的视图,它应该是:

new_user_registration
.

  def check_captcha_on_sign_up
    if CaptchaJob.new.valid?(params[:"smart-token"], request.remote_ip)
      flash.delete :alert
    else
      build_resource(sign_up_params)
      resource.valid?
      clean_up_passwords(resource)
      flash.alert = "Please, confirm you're not a robot"
      respond_with_navigational(resource) do
        redirect_to new_user_registration
      end
    end
  end

您还应该检查 stackoverflow 响应How to integrate recaptcha with devise?注册配置发生在继承自

RegistrationController
.
Devise::RegistrationsController

© www.soinside.com 2019 - 2024. All rights reserved.