显示控制器关注的闪光警报



我有一个控制器问题,它重新组合了访问控制的逻辑:

module AccessControl
extend ActiveSupport::Concern
def restrict
unless current_user
flash.now[:danger] = 'You must log in first'
redirect_to :root
end
end
end

包含在一些控制器中,用作before_action滤波器

class SomeController < ApplicationController
include AccessControl
before_action :restrict
end

但不知何故,flash消息没有显示(其他的flash消息是正确显示,如果他们从控制器调用)。

你知道为什么我不能从Concern中调用flash消息吗?如果它不是预期的/推荐的实现,我怎么能有DRY控制器,但仍然有flash消息显示?

还没有找到activessupport::Concern也没有activedisach::Flash的文档中的答案。我尝试了一些替代方案,但没有一个是真正有效的。

测试一下你的代码,它是有效的。

在您的代码中,flash消息仅在用户未登录时显示,因此请确保在测试时注销。

AJP的回答解决了我的问题。

TLDR:使用flash[:danger]redirect_to

最新更新