Prereq:
- Rails 7.0.4
- Devise 4.9.0 with hotwire/turbo support
- Responders 3.1.0
Devise has the default config with
config.responder.error_status = :unprocessable_entity
config.responder.redirect_status = :see_other
lines in it.
It works properly with standard devise/registrations/new view and processing.
But when I try to add captcha check according to examples, it fails (see below).
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
If I submit sign up form with captcha checked, everything is ok.
But if I submit form without captcha checked, I get this error:
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
Exception causes section contains the message:
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]}.
But the file devise/registrations/new.html.erb
exists.
devise.rb without comments:
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
Of course I tried to add config.navigational_formats = ['*/*', :html, :turbo_stream]
to devise.rb, but it's for previous Devise version and it doesn't work anyway.
How to fix check_captcha_on_sign_up
method to make it render new.html.erb
view properly if captcha is not valid?
Thanks.
UPD: I created a fresh project with fail demo: https://github.com/noff/devise_rails7_captcha_fail_demo