Flash messages missing in a server rendered page

134 views Asked by At

I have a Rails 6 app (migrated from Rails 5 a while ago) which manages Items and an Overview of those Items. When you update an Item you're redirected to it's Overview.

Therefore in my Items Controller:


class ItemsController < ApplicationController
  def update
    respond_to do |format|
      if @item.update(item_params)
        format.html { redirect_to overview_path, notice: "Item updated." }
      else
        format.html { redirect_to overview_path, alert: "An error happend." }
      end
    end
  end
end

I had a requirement to provide a simple API to have this functionality on a micro server which cannot manage much, only display stuff, so no JSON or else. It has to be pure HTML.

It works flawlessly, but with one critical problem: the generated HTML doesn't include the flash message. Tho I have config.middleware.use ActionDispatch::Flash in config/application.rb.

In the Overview Controller, after the redirect_to, puts flash.inspect is empty, and if I add there flash[:notice] = 'Hooray!' it shows, so it's not a render problem.

What am I missing?

1

There are 1 answers

4
Alex On

Best I can figure out from your question is that you're running a rails API server and your controller is an ActionController::API. Assuming that's correct you need several things for flash to work.

Flash messages are stored in session cookies, if you don't have that configured, there is no mechanism to transmit them over the redirect:

# config/application.rb

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore
config.middleware.use ActionDispatch::Flash

and you have to include flash module to have a flash helper and to be able to set flash through redirect_to helper:

class Api::V1::MyController < ActionController::API
  include ActionController::Flash

  #...
end

You can also inspect your session to see stored flash messages, in case you're curious:

<%= session[:flash] %>

there's no browser involved in generating the page

<%== flash.notice.inspect %>
<%== session[:flash] %>

Without cookies:

$ curl http://localhost:3000/items -L -d ""
nil

With cookies:

$ curl http://localhost:3000/items -L -d "" -c cookie.txt -b cookie.txt
"CREATED"
{"discard"=>[], "flashes"=>{"notice"=>"CREATED"}}