Ruby-on-rails Rails Flash.now 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7133392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails Flash.now not working
提问by Manjunath Manoharan
I have a view from which I make an ajax request to the controller and after the action is successfully completed I initialize the flash.now[:notice]. But after the control goes back to the view. I don't happen to see the flash message.
我有一个视图,从中我向控制器发出 ajax 请求,并在操作成功完成后初始化flash.now[:notice]。但是在控件返回视图之后。我没有看到闪现信息。
flash.now[:notice] = "Request Completed successfully" if @meetings.any?
回答by AJP
回答by Arne Cordes
Do you flash.now BEFORE you call render? Otherwise your message won′t appear.
在您调用渲染之前,您是否使用 flash.now?否则您的消息将不会出现。
回答by fmnoise
code in the controller:
控制器中的代码:
flash[:success] = "All good!"
format.html { redirect_to some_path}
and in the view with close button:
并在带有关闭按钮的视图中:
<% flash.each do |key, value| %>
<%= content_tag(:div, class: "alert alert-#{key} alert-dismissable") do %>
<%= value %>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<% end %>
<% end %>
回答by Andrea
Check you've got something like
检查你有类似的东西
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
in your application.html.erb file: if you don't you must add it, as this is where the notice will be displayed.
在 application.html.erb 文件中:如果不添加,则必须添加它,因为这是显示通知的位置。

