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" %>
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
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
redirect_to new_user_registrations_path, :notice => "This flash will show up on the sign up page"
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
class UsersController < ApplicationController
def custom_action
@user = User.find params[:id]
do_something_with @User
flash[:user] = "Custom action completed!"
redirect_to @user
end
end
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.
<%= 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