Blog index

Fixing flash not appearing on Rails 8

Dibuat pada:

Suppose I have this code:


  def create
    if user = User.authenticate_by(params.permit(:email_address, :password))
      start_new_session_for user
      redirect_to after_authentication_url, notice: "You have successfully logged in!"
    else
      redirect_to new_session_url, alert: "Try another email address or password."
    end
  end

And for flash, it is displayed like this. It is the default flash from the Administrate gem.

<% if flash.any? %>
  <div class="flashes">
    <% flash.each do |key, value| -%>
      <% next unless value.respond_to?(:html_safe) %>
      <div class="flash flash-<%= key %>"><%= value.html_safe %></div>
    <% end -%>
  </div>
<% end %>

However, no matter what I did, the flash did not appear. Even though in the comment section I saw the rendered flash message.

Root cause

It appears that the bug was caused by conflicting rendering with Turbo. After I disabled Turbo, the flash message now appears.

<%= form.submit value: "Sign in", class: "button button--primary", data: { turbo: false } %>

Notice, I added data: { turbo: false } to the submit button to prevent using Turbo.