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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:49:18  来源:igfitidea点击:

Rails Flash.now not working

ruby-on-railsruby-on-rails-2

提问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

When redirecting use

重定向使用时

flash[:notice] = "This message value is available in next request-response cycle"

When rendering use

渲染时使用

flash.now[:notice] = "Message is available in same request-response cycle"

Info from here

来自这里的信息

回答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">&times;</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 文件中:如果不添加,则必须添加它,因为这是显示通知的位置。