我試圖設置reCaptcha與Devise,並且我想添加一個篩選器,在允許用戶創建之前檢查verify_recaptcha是否爲true。如果它是錯誤的,則不應該創建用戶。devise before_action:在創建用戶之前驗證reCaptcha
我試圖按照設計的documentation如何做到這一點,但它似乎並不適用於我的情況。我也在生產模式下測試recaptcha,並且一般設置工作,我可以檢查reCaptcha是真是假。
這就是設計告訴我在RegistrationsController做:
prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect.
private
def check_captcha
unless verify_recaptcha
self.resource = resource_class.new sign_up_params
respond_with_navigational(resource) { render :new }
end
end
沒有工作。林100%確定verify_recaptcha方法的工作原理,並沒有錯誤的設置。
我已經試過:
class RegistrationsController < Devise::RegistrationsController
before_action :check_captcha
private
def check_captcha
if !verify_recaptcha == true
flash[:recaptcha_error] = "Fejl. Husk at sætte flueben i at du ikke er en robot"
signupuser_users_path
end
end
末
,但沒有運氣。我將如何做到這一點?在用戶註冊之前,設計通常會處理哪些驗證方法需要運行?
我想在你的控制器,你應該把這個'prepend_before_action:check_captcha,只有:[:創建]的''而不是before_action:CHEC k_captcha',因爲我認爲你只需註冊 – Sunny
謝謝。我最終得到了devise的方法來使用我的回答中的代碼 – sneglefar
你可以給我評論我的評論 – Sunny