简体   繁体   English

Rails警告不起作用

[英]Rails alert not working

I am trying to alert a user if they are logged in or not to an app. 我试图提醒用户他们是否登录到应用程序。 Here is my sessions destroy action: 这是我的会话销毁行动:

  def destroy
    session[:user_id] = nil
    redirect_to root_url, alert: "Logged out!"
  end

Pretty strait forward. 漂亮的海峡前进。 One somebody clicks the logout button, I would like an alert to pop u saying "Logged out!". 有人点击退出按钮,我想要一个警告弹出你说“退出!”。

It isn't showing up, but I also believe I am missing something in the view. 它没有出现,但我也相信我在视图中遗漏了一些东西。 What do I have to do in the views for this to show? 我要在这个视图中做些什么来展示?

Your view template should conditionally render a flash message if one is in the session. 如果会话中有视频模板,则视图模板应有条件地呈现Flash消息 The following snippet will display all alert flash messages: 以下代码段将显示所有alert Flash消息:

<% if flash[:alert] %>
    <div class="alert"><%= flash[:alert] %></div>
<% end %>

UPDATE : 更新

If, alternatively, you would like to render out any flash message type (eg, alert , notice , warning , etc.), you can use the following snippet to iterate through any and all of the flash messages in your session: 或者,如果您要渲染任何 Flash消息类型(例如, alertnoticewarning等),您可以使用以下代码段迭代会话中的任何和所有 Flash消息:

<% flash.each do |key, value| %>
    <div class="<%= key %>"><%= value %></div>
<% end %>

Assuming you utilize more than one flash message type, this latter approach is more dynamic and requires less code to handle than handling each flash message type individually. 假设您使用多种闪存消息类型,后一种方法比单独处理每种闪存消息类型更动态,需要的代码更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM