Rails(5.2.0)reCaptcha Devise(4.4.3)undefined方法users_url

时间:2018-05-22 21:40:19

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

测试时出错。开发和生产没有错误。

Error:
UsersTest#test_creating_a_user:
NoMethodError: undefined method `users_url' for #<RegistrationsController:0x00007fd3e69d1a18>
Did you mean?  user_session_url
Did you mean?  user_session_url
    app/controllers/registrations_controller.rb:11:in `check_captcha'

有一个设计注册控制器:

class RegistrationsController < Devise::RegistrationsController
   prepend_before_action :check_captcha, only: [:create]

   private

def check_captcha
  unless verify_recaptcha
    self.resource = resource_class.new sign_up_params
    resource.validate # Look for any other validation errors besides Recaptcha
    set_minimum_password_length
    respond_with resource
    # self.resource = resource_class.new sign_up_params
    # respond_with_navigational(resource) { render :new }
  end 
end

def after_inactive_sign_up_path_for resource
  new_user_session_path
end
end

路由

Rails.application.routes.draw do
  devise_for :users, only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'omniauth_callbacks'}

scope'(:locale)',: locale =&gt; / en | ru / do

root :to => 'index#index'

devise_for :users, :controllers => {
                    :registrations => 'registrations',
                    :invitations => 'invitations'
                   },
                   :path => 'auth',
                   :path_names => {
                     :sign_in => 'login',
                     :sign_out => 'logout',
                     :password => 'secret',
                     :confirmation => 'verification',
                     :unlock => 'unblock',
                     :registration => 'register',
                     :sign_up => 'cmon_let_me_in'
                   }, 
                   skip: :omniauth_callbacks

   end

传递给respond_with resource的资源有效。 那是什么?!?!

4 个答案:

答案 0 :(得分:1)

我认为是因为你的意见。

结帐您的观看次数“/devise/registrations/new.html.erb”,并确保将<%= recaptcha_tags %>放在注册按钮上/devise/registrations/new.html.erb

这是我的简单路线: routes.rb

答案 1 :(得分:0)

现在绕过它

def check_captcha
  return true if Rails.env.test?
  unless verify_recaptcha
    self.resource = resource_class.new sign_up_params
    resource.validate # Look for any other validation errors besides Recaptcha
    set_minimum_password_length
    respond_with resource
  end
end

答案 2 :(得分:0)

如果没有recaptcha,

也无效,检查设计文档以使其独立工作

答案 3 :(得分:0)

当资源本身有效但验证码没有通过时,会发生此错误。 Devise认为该模型是持久的(但仅经过验证),并尝试重定向到该模型。

解决方案是用respond_with resource方法将render :new替换为check_captcha