gpt4 book ai didi

ruby-on-rails - 在 Rails 中通过单击让 Flash 消息消失

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:56 25 4
gpt4 key购买 nike

我在整个应用程序中都有标准的 Flash 消息,使用 Bootstrap 进行样式设置。这些工作正常,但我想添加一个功能。
在他/她的屏幕上收到一条消息的访问者应该能够点击闪消息离开/使闪消息消失。例如,如果单击右上角的 X,则会删除 Flash 消息。 这可能吗(没有 javascript)? 我在下面添加了我当前的代码,它不允许这种行为。我应该如何针对这种行为调整我的代码?


目前,例如,我的 Controller 中有:

flash.now[:info] = "Your subscription has expired"

在 application.html.erb 中:

<% flash.each do |message_type, message| %>
<%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
<% end %>

我有部分 _error_messages.html.erb:

<% if object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(object.errors.count, "error") %>.
</div>
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg.html_safe %></li>
<% end %>
</ul>
</div>
<% end %>

最佳答案

试着像这样给它

<div class="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<% flash.each do |message_type, message| %>
<%= content_tag(:div, message, class: "alert alert-#{message_type}") %>
<% end %>
</div>

更新

这应该可以工作

<% flash.each do |message_type, message| %>
<%= content_tag :div, class: "alert alert-#{message_type}" do -%>
<%= message %>
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<% end -%>
<% end %>

关于ruby-on-rails - 在 Rails 中通过单击让 Flash 消息消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com