简体   繁体   English

Ruby on Rails Flash通知错误

[英]Ruby on rails flash notice error

I have a problem with flash[:notice] = "Message" in Ruby on Rails. 我在Ruby on Rails中使用flash[:notice] = "Message"遇到问题。

I am trying to create login fault error message. 我正在尝试创建登录错误错误消息。 My login fault handling is: 我的登录错误处理是:

flash[:notice] = "Invalid username/password combination."
redirect_to(:action => 'login')

For the reason I don't know, alert just doesn't show up. 由于我不知道的原因,警报不会出现。 I have red tons of possible solutions, but all of them just doesn't work for me. 我有很多可行的解决方案,但它们对我都不起作用。 I am using Safari / Google Chrome web browsers. 我正在使用Safari / Google Chrome浏览器。

Your controller code looks fine. 您的控制器代码看起来不错。 I suspect your problem is not that you are calling the flash method incorrectly in your controller, but rather that you did not write the code (correctly) to display the flash in your view. 我怀疑您的问题不是您在控制器中错误地调用了flash方法,而是您没有(正确地)编写代码以在视图中显示flash。

flash simply holds a message for your views to display. flash只是保留一条消息以供您显示视图。 You are setting the message correctly, but you may not be displaying it properly. 您正在正确设置该消息,但是可能显示不正确。 I can't tell because you aren't posting your view code. 我不知道是因为您没有发布视图代码。

For example, if this action is the result of submitting a user/login.html.erb form, based on your controller code, you would want to have the following code inside that view file: 例如,如果此操作是基于控制器代码提交user/login.html.erb表单的结果,则您希望该视图文件中包含以下代码:

<% flash.each do |key, value| %>
  <div class="alert"><%= value %></div>
<% end %>

This code is a more basic version of what is described in this article . 此代码是本文描述的内容的更基本版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM