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?
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:
and you have to include flash module to have a
flashhelper and to be able to set flash throughredirect_tohelper:You can also inspect your session to see stored flash messages, in case you're curious:
Without cookies:
With cookies: