Ruby-on-rails 如何在重定向时显示 Rails Flash 通知?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15534128/
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
How to display a Rails flash notice upon redirect?
提问by at.
I have the following code in a Rails controller:
我在 Rails 控制器中有以下代码:
flash.now[:notice] = 'Successfully checked in'
redirect_to check_in_path
Then in the /check_in view:
然后在 /check_in 视图中:
<p id="notice"><%= notice %></p>
However, the notice does not show up. Works perfect if I don't redirect in the controller:
但是,该通知并未显示。如果我不在控制器中重定向,则工作完美:
flash.now[:notice] = 'Successfully checked in'
render action: 'check_in'
I need a redirect though... not just a rendering of that action. Can I have a flash notice after redirecting?
不过,我需要重定向……而不仅仅是该操作的渲染。重定向后我可以收到闪现通知吗?
回答by Rebitzele
Remove the ".now". So just write:
删除“.now”。所以只需写:
flash[:notice] = 'Successfully checked in'
redirect_to check_in_path
The .now is specifically supposed to be used when you are just rendering and not redirecting. When redirecting, the .now is not to be used.
当您只是渲染而不是重定向时,特别应该使用 .now。重定向时,不使用 .now。
回答by Tauqeer Ahmad
redirect_to new_user_session_path, alert: "Invalid email or password"
in place of :alertyou can use :notice
代替:alert你可以使用:notice
to display
显示
回答by etlds
Or you can do it in one line.
或者您可以在一行中完成。
redirect_to check_in_path, flash: {notice: "Successfully checked in"}
回答by Jon Schneider
If you are using Bootstrap, this will display a nicely-formatted flash message on the page that's the target of your redirect.
如果您使用的是 Bootstrap,这将在您重定向的目标页面上显示格式良好的 Flash 消息。
In your controller:
在您的控制器中:
if my_success_condition
flash[:success] = 'It worked!'
else
flash[:warning] = 'Something went wrong.'
end
redirect_to myroute_path
In your view:
在您看来:
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
<% end %>
This will produce HTML like:
这将产生如下 HTML:
<div class="alert alert-success">It worked!</div>
For available Bootstrap alert styles, see: http://getbootstrap.com/docs/4.0/components/alerts/
有关可用的 Bootstrap 警报样式,请参阅:http: //getbootstrap.com/docs/4.0/components/alerts/
Reference: https://agilewarrior.wordpress.com/2014/04/26/how-to-add-a-flash-message-to-your-rails-page/
参考:https: //agilewarrior.wordpress.com/2014/04/26/how-to-add-a-flash-message-to-your-rails-page/
回答by Seifer
This will work too
这也行
redirect_to check_in_path, notice: 'Successfully checked in'
redirect_to check_in_path, notice: 'Successfully checked in'
回答by Florencio Lugo
I had the same problem, and your question solved mine, because I had forgotten to include in the /check_in view:
我遇到了同样的问题,你的问题解决了我的问题,因为我忘记包含在 /check_in 视图中:
<p id="notice"><%= notice %></p>
In the controller, just a single line:
在控制器中,只有一行:
redirect_to check_in_path, :notice => "Successfully checked in"

