ruby-on-rails 导轨7+设计+验证码:未定义#的方法“users_url”< Devise::RegistrationsController:0x00000000036600>

avwztpqn  于 2023-03-04  发布在  Ruby
关注(0)|答案(1)|浏览(146)

先决条件:

  • 导轨7.0.4
  • 设计4.9.0,带热线/涡轮支持
  • 应答者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存在。
devies.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]到device.rb,但它是为以前的Devise版本,它不工作反正。
如何修复check_captcha_on_sign_up方法,使其渲染new.html.erb视图正确,如果验证码是无效的?
谢谢。
UPD:我创建了一个新的项目失败演示:https://github.com/noff/devise_rails7_captcha_fail_demo

1szpjjfi

1szpjjfi1#

在你的代码中,你要求从你的ApplicationController渲染:new,正如它在2.1-渲染一个动作的视图Layouts and Rendering in Rails中所说的,它自动渲染与控制器关联的视图。你的代码发生在ApplicationController中,我认为它试图从ApplicationController重新渲染new视图。
因此,您需要将用户发送到由device控制器生成的视图,它应该是: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

您还应该检查堆栈溢出响应How to integrate recaptcha with devise?,其中注册配置发生在从Devise::RegistrationsController继承的RegistrationController中。

相关问题