关于设计可锁定的第n次尝试的自定义消息

我正在寻找一种方法来显示设备可锁定模块中的各种失败尝试的自定义消息。

我发现默认情况下只能通知最后一次尝试: https : //github.com/plataformatec/devise/blob/master/lib/devise/models/lockable.rb#L121

是否有任何优雅的方式来显示第7次和第8次失败尝试的不同警告信息? 你能提出一个吗?

也许是一个自定义的SessionsController,我猜这个设计使用动态方法,我找不到:调用last_attempt只是覆盖unauthenticated_message方法

def create if self.resource.failed_attempts != self.resource.class.maximum_attempts - 1 and self.resource.failed_attempts != 0 attempts_left = self.resource.failed_attempts flash[:alert] = I18n.t 'some_path.errors.attempts', attempts_left: attempts_left super end 

想法是在最大尝试次数为1次或小于最大次数时添加闪光警报,在最大尝试次数和最后一次尝试的情况下,您将获得默认消息

这个想法很有意思,但是因为在create之前调用了lockable ,所以我无法工作,所以我创建了一个自定义动作,我在create之前调用它。

这对我有用:

 class User::SessionsController < Devise::SessionsController before_action :check_failed_attempts, only: :create def after_sign_in_path_for(user) # some code end def create super # create some stuff and check others end def destroy # may be destroy and deal with cookies super end def check_failed_attempts flash.clear email = self.params["user"]["email"] return unless email user = User.find_by_email(email) return unless user failed_attempts = user.failed_attempts maximum_attempts = User.maximum_attempts attempts_left = maximum_attempts - failed_attempts - 1 if attempts_left != maximum_attempts and attempts_left > 1 flash[:notice] = I18n.t 'devise.failure.n_attempt', attempts_left: attempts_left end end end