Mic*_*ley 70
可能最简单的方法是
在您的devise.en.yml文件中,将每条消息指定为空:
en:
errors:
messages:
not_found: ''
already_confirmed: ''
not_locked: ''
Run Code Online (Sandbox Code Playgroud)
接下来,在您的布局中,在输出之前检查空白闪存字符串.
<% flash.each do |key, value| %>
<%= content_tag :div, value, :class => "flash #{key}" unless value.blank? %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
acr*_*sis 34
更适合我的答案是像这样覆盖Devise Session Controller
class SessionsController < Devise::SessionsController
# POST /resource/sign_in
def create
super
flash.delete(:notice)
end
# DELETE /resource/sign_out
def destroy
super
flash.delete(:notice)
end
end
Run Code Online (Sandbox Code Playgroud)
这安全地覆盖了删除flash消息的create和destroy方法
这对我有用:
# app/controllers/users/sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
after_action :remove_notice, only: [:destroy, :create]
private
def remove_notice
flash.discard(:notice) #http://api.rubyonrails.org/v5.1/classes/ActionDispatch/Flash/FlashHash.html#method-i-discard
end
end
# add this line in 'config/routes.rb'
devise_for :users, :controllers => { sessions: 'users/sessions' }
Run Code Online (Sandbox Code Playgroud)
我使用Users::SessionsController但你可以使用SessionsController,在这个例子中我只有一个设计模型。
我使用flash.discard(:notice)但您可以flash.discard同时删除其他类型。(方法丢弃自 rails 3.0 开始存在)
我更喜欢这种方法,因为检查您的 Flash 消息是否为空白不是视图的作用。如果您有闪现信息,请打印出来!如果您不想要,那么请不要创建 Flash 消息 ;-)
我已经能够通过覆盖在给定的控制器中禁用它们is_flashing_format?:
def is_flashing_format?
false
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Devise 3.5.6
| 归档时间: |
|
| 查看次数: |
15047 次 |
| 最近记录: |