设计闪现消息不显示
Devise 在我的应用程序中完美运行,除了来自 devise.en.yml 的闪存消息未显示在视图中。我做错了什么?
下面是我的注册页面视图,我尝试了 :alert
和 :notice
但不起作用。
先感谢您
<h2>Sign up</h2>
<% if flash[:alert] %>
<%=flash[:alert]%>
<%end%>
<%= form_for(resource,:as=>resource_name,:url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :Username %></p>
<p><%= f.text_field :username %></p>
<p><%= f.label :email %></p>
<p><%= f.text_field :email %></p>
<p><%= f.label :password %></p>
<p><%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %></p>
<p><%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
Devise works perfectly in my app except for flash messages from devise.en.yml doesn't get displayed in the view. What am I doing wrong?
Below is my sign up page view i have tried both :alert
and :notice
but not working.
Thank you in advance
<h2>Sign up</h2>
<% if flash[:alert] %>
<%=flash[:alert]%>
<%end%>
<%= form_for(resource,:as=>resource_name,:url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :Username %></p>
<p><%= f.text_field :username %></p>
<p><%= f.label :email %></p>
<p><%= f.text_field :email %></p>
<p><%= f.label :password %></p>
<p><%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %></p>
<p><%= f.password_field :password_confirmation %></p>
<p><%= f.submit "Sign up" %></p>
<% end %>
<%= render :partial => "devise/shared/links" %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您看不到任何闪光,因为闪光灯未设置为任何内容。您似乎误解了闪光灯的用途。
Flash 的一般行为是在当前请求中将其设置为用户当前会话中的值,然后在后续请求中显示该 Flash 消息。此模式允许执行以下操作:设置 Flash、重定向到另一个页面,然后将该 Flash 显示在从重定向加载的该页面上。然后闪存被消耗并从会话中删除。
例外情况是使用 flash.now,它使 Flash 可从当前操作而不是后续操作中使用。
在您的情况下,闪光灯未显示,因为没有可显示的闪光灯。加载注册页面不会设置任何闪现消息。闪光灯在您的视图中具有值的唯一方法是,如果其他操作重定向到它,则在执行此操作之前设置闪光灯。像这样的东西
通常您不想在特定视图中调用闪光灯,而是在应用程序布局中调用闪光灯。这允许通过任何执行重定向的操作来设置 Flash,并且 Flash 将显示在后续页面上的任何内容上。从特定视图设置闪光灯需要知道该特定视图将是重定向中使用的视图。情况可能并不总是如此,flash.now 是一个例外,因为它不能脱离重定向。如果我确实需要在特定视图中使用 Flash,我不会使用传统的警报/通知 Flash 变量,因为我会在应用程序布局中查找这些变量,并且会导致 Flash 渲染两次。相反,我可能会将闪光灯设置为类似的内容
,然后在我的用户/显示视图中,我会查找 flash[:custom] 并使用该闪光灯执行某些操作,该操作的处理方式与布局中的闪光灯处理方式不同。我实际上没有必要做这样的事情,但如果我必须这样做,我可能会这样做。
Your not seeing any flash because the flash is not being set to anything. It seems like your misinterpreting the purpose of the flash.
The general behavior of the flash is to be set to a value in the user's current session in the current request, then on a subsequent request, that flash message is displayed. This pattern allows for actions to set the flash, redirect to another page, then have that flash displayed on that page that loads from the redirection. The flash is then consumed and removed from the session.
The exception to this is to use flash.now which makes the flash available from the current action, instead of a subsequent action.
In your case, the flash is not displaying because there is no flash to display. Loading the signup page does not set any flash message. The only way the flash would have a value in your view would be if some other action redirected to it, setting the flash before it did so. Something like this
Normally you don't want to call the flash within a specific view, but rather in your application layout. This allows the flash to be set from any action that does a redirection and the flash will show up on whatever the subsequent page happens to be. Setting the flash from a specific view would require knowing that the specific view will be the one used in the redirection. This might not always be the case, flash.now would be an exception since it doesn't work off of a redirection. If I did need to use the flash in a specific view, I would not use the conventional alert/notice flash vars, since those I would look for in my application layout and would cause my flash to be rendered twice. Instead I might set the flash to something like
Then in my users/show view I would look for flash[:custom] and do something with that flash that is handled differently than the flash handling in my layout. I haven't actually had to do anything like this, but if I were to have to, this is how I might handle it.
你试过把闪光灯的东西拿出来吗? devise_error_messages 应该处理它们。我的没有它也可以工作,原始的设计视图也没有它:https://github.com/plataformatec/devise/blob/master/app/views/devise/registrations/new.html.erb
Have you tried taking out the flash stuff ? The devise_error_messages should take care of them. Mine works without it, and the original devise view doesn't have it either: https://github.com/plataformatec/devise/blob/master/app/views/devise/registrations/new.html.erb
添加涡轮配置为我解决了这个问题。您可以通过两种方式做到这一点。
将此行代码添加到 config/initializers/devise.rb 文件
config.navigational_formats = ['/', :html, :turbo_stream]
或者添加 {"data-turbo": " false"} 的形式不渲染 flash。在这个问题中,正确的代码行是
<%= form_for(resource,:as=>resource_name,:url =>registration_path(resource_name), data: { Turbo: false }) do |f| %>
Adding turbo configurations solved this issue for me. You can do it in two ways.
Add this line of code to the config/initializers/devise.rb file
config.navigational_formats = ['/', :html, :turbo_stream]
Or add {"data-turbo": "false"} in the form that is not rendering flash. In this question, the correct line of code would be
<%= form_for(resource,:as=>resource_name,:url => registration_path(resource_name), data: { turbo: false }) do |f| %>
<%= 渲染:部分 => “共享/flash_messages”%>
或者
=render 'shared/flash_messages'(如果人们使用 Haml)
在视图文件中查看错误消息或设计:failure: messages
<%= render :partial => "shared/flash_messages" %>
or
=render 'shared/flash_messages' (if people are using Haml)
in your view file to view the error messages or devise: failure: messages