Home > database >  Why is my Flash.now not outputting after a render?
Why is my Flash.now not outputting after a render?

Time:04-29

I have a minor issue I can't seem to figure out.

Regular flash works just as intended.

flash[:test] = 'Blarrgh'
redirect_to etcs_url

Will display 'Blarrgh' since I have this code in my application.html.erb

      <% if flash[:test] %>
        <%= flash[:test] %>
      <% elsif flash.now[:test] %>
        <%= flash.now[:test] %>
      <% end %>

I'm pretty sure the way to display flash.now[:test] messages is just flash[:test], but I added the elsif in the bottom just in case since things weren't working.

For some reason, when I do

flash.now[:test] = 'Blarrgh'
render :edit

Nothing shows up at all. But when I put a 'fail' statement into my view then check out the values, flash[:test]/flash.now[:test] will have the proper values. E.g, ['Etc can't be blank']

I tried checking out common errors: no CSRF token, hidden redirect, flash.now b4 render, using form_with without local:true, but looking at my logs, there's no such issue. I included the CSRF token and my log path is

In logs:
patch /etc/1
(Commit fails, rolls back)
renders application.html.erb which yields etcs/edit.html.erb which renders etcs/_form.html.erb, then renders rest of etcs/edit.html.erb

And I'm putting my flash.now b4 the render. I'm also not using Rails helper functions like form_with.

CodePudding user response:

Never mind, I figured it out. Rails is absolutely insane sometimes, apparently.

If you do a

Flash.now[:errors] = 'This is an error'
render :edit

This won't work. You have to do

Flash.now[:errors] = 'This is an error'
render :edit, status: :whatever_status

This doesn't show up as an error in the browser console, for some reason. It just simply doesn't display your flash.now. Supposedly, it's an issue with Rails 7's turbo being demanding as hell. I hope this saves someone from dealing with an awful lot of pain and misery.

Found the answer from Alex's reply in Flash is not displayed in the same view in Rails

  • Related