Ruby-on-rails 在不重定向的情况下在 rails 控制器中引发警报
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7534889/
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 02:01:31 来源:igfitidea点击:
Raise an alert in rails controller without redirect
提问by hagope
I just want to flash an notice/error if the message is/isn't saved, without any redirect, how can I have no redirect:
如果消息被/未保存,我只想闪现一个通知/错误,没有任何重定向,我怎么能没有重定向:
respond_to do |format|
if @message.save
format.html { redirect_to request.referer, :notice => 'Message sent!' } #dont want redirect
else
# error message here
end
回答by Benoit Garret
Use flash.now:
使用flash.now:
if @message.save
flash.now[:notice] = 'Message sent!'
else
flash.now[:alert] = 'Error while sending message!'
end
respond_to do |format|
format.html { # blahblah render }
end
回答by Obromios
In rails 5, you can do:
在 Rails 5 中,您可以执行以下操作:
format.html { redirect_to request.referer, alert: 'Message sent!' }

