Ruby on rails Rails如何在响应后显示flash消息

Ruby on rails Rails如何在响应后显示flash消息,ruby-on-rails,devise,activeadmin,flash-message,Ruby On Rails,Devise,Activeadmin,Flash Message,在active admin中,我必须覆盖Desive password controller,以便在密码令牌过期时显示错误。默认情况下,如果令牌过期,它不会显示任何错误。下面是我要重写的方法 # PUT /resource/password def update self.resource = resource_class.reset_password_by_token(resource_params) yield resource if block_given?

在active admin中,我必须覆盖Desive password controller,以便在密码令牌过期时显示错误。默认情况下,如果令牌过期,它不会显示任何错误。下面是我要重写的方法

    # PUT /resource/password
  def update
    self.resource = resource_class.reset_password_by_token(resource_params)
    yield resource if block_given?

    if resource.errors.empty?
      resource.unlock_access! if unlockable?(resource)
      if Devise.sign_in_after_reset_password
        flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
        set_flash_message(:notice, flash_message) if is_flashing_format?
        sign_in(resource_name, resource)
      else
        set_flash_message(:notice, :updated_not_active) if is_flashing_format?
      end
      respond_with resource, location: after_resetting_password_path_for(resource)
    else
      set_minimum_password_length
      respond_with resource
    end
  end
如果
resource.errors.empty?
返回false,则不会设置任何闪存消息。 为了抛出错误异常,我执行了以下操作:

ActiveAdmin::Devise::PasswordsController.class_eval do
  def update
    super
    if resource.errors.any?
      flash[:notice] = resource.errors.full_messages.to_sentence
    end
  end
end
yield resource if block_given?
使用上述代码,错误现在是可见的,但不在相同的页面加载上。但是,它将显示在下一个视图中。如果我从Desive password controller复制代码,并在“respond_with”之前的else块中添加flash消息,也可以正常工作,但我不喜欢这种方法


有没有一种方法可以显示flash消息,而无需从设备控制器复制整个方法?

在更新操作的第二行中,有以下内容:

ActiveAdmin::Devise::PasswordsController.class_eval do
  def update
    super
    if resource.errors.any?
      flash[:notice] = resource.errors.full_messages.to_sentence
    end
  end
end
yield resource if block_given?
这意味着您可以将块传递给此方法,如下所示:

def update
  super do |resource|
    if resource.errors.any?
      flash[:notice] = resource.errors.full_messages.to_sentence
    end
  end
end

这也行

def create
 @user = User.new(params[:user])

 respond_to do |format|
 if @user.save
  flash[:notice] = 'User was successfully created.'
  format.html { redirect_to(@user) }
  format.xml { render xml: @user, status: :created, location: @user }
else
  format.html { render action: "new" }
  format.xml { render xml: @user.errors, status: :unprocessable_entity           }
 end
 end
 end

你试过flash.now吗?flash.now根本不显示任何错误。