Ruby-on-rails flash[:notice] 在 Rails 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1779405/
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
flash[:notice] not working in Rails
提问by Shiv
I have the following snippet of code in my controller
我的控制器中有以下代码片段
def create
@message = Message.new(params[:message])
@message.message = h(@message.message)
if @message.save
flash[:message] = "Message Sent. Thank You for Contacting Me"
else
flash[:message] = "OOps Something went wrong"
end
redirect_to :action => 'contact'
end
When I try to display the flash message in the contact form it doesnot display. I looked up possible solutions, but they dont seem to work. Any ideas what is going wrong?
当我尝试在联系表单中显示 Flash 消息时,它不显示。我查找了可能的解决方案,但它们似乎不起作用。任何想法出了什么问题?
回答by nowk
Your controller is redirecting to :action => 'contact'. Ensure that the template being rendered for that action has the flash notice output.
您的控制器正在重定向到 :action => 'contact'。确保为该操作呈现的模板具有 Flash 通知输出。
<%= flash[:message] %>
Also, you may want to use render :action ... vs redirect_to :action .... Save yourself a request.
此外,您可能想要使用 render :action ... vs redirect_to :action .... 为自己保存一个请求。
回答by EmFi
The flash hash can contain any set of messages you want to save until the next render. Scaffolds usually use the contents of flash[:notice] for notification messages. If you didn't use a scaffold to generate your web page you will have to add <%= flash[:notice]%>to your views.
闪存哈希可以包含您想要保存到下一次渲染的任何消息集。脚手架通常使用 flash[:notice] 的内容作为通知消息。如果您没有使用脚手架来生成您的网页,您将不得不添加<%= flash[:notice]%>到您的视图中。
You're setting flash[:message] in your controller. So it's not going to show up anywhere in your view unless your view contains <%= flash[:message]%>somewhere.
您正在控制器中设置 flash[:message]。因此,除非您的视图包含<%= flash[:message]%>某处,否则它不会出现在您视图中的任何地方。
Your possible solutions are change all occurrences of flash[:message]to flash[:notice]in your controller or add <%= flash[:message]%>to any views that this action could render.
您可能的解决方案是更改控制器中所有出现的flash[:message]toflash[:notice]或添加<%= flash[:message]%>到此操作可能呈现的任何视图。
回答by Anand Shah
Not saying that you wouldn't have tried it, but if I were you I would do something down the lines like
并不是说你不会尝试,但如果我是你,我会做一些类似的事情
<% if flash[:messsage].blank? %>
<h1> flash hash is blank </h1>
<% end %>
If you see the "flash hash is blank" in your browser you know what it means.
如果您在浏览器中看到“flash hash is blank”,您就知道这意味着什么。
EDIT:-
编辑:-
Something from the docs "Just remember: They‘ll be gone by the time the next action has been performed." Try this in your controller
文档中的一些内容“请记住:执行下一个操作时,它们将消失。” 在你的控制器中试试这个
flash.keep(:message) #keep the flash entry available for the next action
回答by Andrew
I recently ran into a scenario where flash messages will not be preserved. This scenario is when submitting a form which does not contain the CSRF token field. Rails will not be able to verify the CSRF token, so it will not preserve the flash messages. If you are using a plain HTML form, use the form_taghelper to automatically add this field for you.
我最近遇到了不保留 Flash 消息的情况。这种情况是在提交不包含 CSRF 令牌字段的表单时。Rails 将无法验证 CSRF 令牌,因此它不会保留 flash 消息。如果您使用的是纯 HTML 表单,请使用form_tag帮助程序自动为您添加此字段。
<%= form_tag '/example/url' do %>
<input name="example">
<input type="submit" value="Submit the Form">
<% end %>

