Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise flash messages don't show up

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" %>
like image 968
katie Avatar asked Oct 30 '11 21:10

katie


People also ask

What is a flash error message?

A flash message is a way to communicate information with the users of your Rails application so they can know what happens as a result of their actions. Example messages: “Password changed correctly” (confirmation) “User not found” (error)

What does devise Authenticate_user do?

Devise also comes with some very useful helper functions: before_action :authenticate_user! — Add to any controller to limit access to an action unless a user is logged in.

What does flash do in ruby?

To implement such messages, Rails provides the flash. The flash is basically a hash that controllers and ERBs can read and write. The flash is part of the session, so the data stored in the flash are specific to a particular user session.

Does devise work with Rails 7?

That should do it! Our out-of-the box Devise setup is now working with Rails 7.


2 Answers

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.

like image 140
Cluster Avatar answered Sep 18 '22 18:09

Cluster


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

like image 38
cbron Avatar answered Sep 17 '22 18:09

cbron