1

I have a problem with flash[:notice] = "Message" in Ruby on Rails.

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.

4

1 回答 1

2

您的控制器代码看起来不错。我怀疑您的问题在于您在控制器中错误地调用了该flash方法,而是您没有(正确地)编写代码以在视图中显示闪存。

flash只保留一条消息供您的视图显示。您正在正确设置消息,但您可能无法正确显示它。我不知道,因为您没有发布您的视图代码。

例如,如果此操作是提交user/login.html.erb表单的结果,根据您的控制器代码,您可能希望在该视图文件中包含以下代码:

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

此代码是本文中描述的更基本的版本。

于 2015-06-27T18:54:51.133 回答