【问题标题】:alert not showing up when rendering an action in Rails在 Rails 中渲染动作时没有显示警报
【发布时间】:2012-09-20 01:40:33
【问题描述】:

我编写了以下代码来显示带有警报闪烁消息的show.html.erb 视图:

render action: 'show', alert: 'Please fix mistakes outlined in red'

但是,警报没有出现。通知出现了。我假设我上面的render action: 'show' 调用是错误的,因为我只是凭直觉写的。这是我的 html.erb 代码:

<%if notice%><p id="notice"><%= notice %></p><%end%>
<%if alert%><p id="alert"><%= alert %></p><%end%>

【问题讨论】:

  • 您是从控制器操作中调用“渲染”吗?

标签: ruby-on-rails ruby-on-rails-3 controller ruby-on-rails-3.2 alert


【解决方案1】:

处理闪烁的更标准方法是在视图布局中编写以下内容

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

在你的控制器中写下以下内容

flash[:alert] = 'Please fix mistakes outlined in red'
render 'show'

【讨论】:

  • flash[:alert] 设置为我的字符串效果很好。所以我猜render 方法不像redirect_to 方法那样采用alert 哈希条目。为什么上面的flash.each机制更标准? Rails 的脚手架机制有id="notice" 消息,devise 有id="notice"id="alert" 通知机制。
猜你喜欢
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
  • 2018-03-28
  • 2020-07-06
  • 1970-01-01
  • 2012-10-23
相关资源
最近更新 更多