-
Notifications
You must be signed in to change notification settings - Fork 329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep flash in Turbo Frame requests #699
Conversation
A POST request can set the flash and return a redirect response. If a Turbo-Frame request that gets made before the redirect location gets called, the flash gets discared by the Turbo-Frame request. By making sure a Turbo-Frame request keeps the flash, we can avoid this problem.
message = Message.create! | ||
|
||
post messages_path | ||
assert_equal @request.flash[:notice], 'Message was successfully created.' | ||
|
||
get messages_path, headers: { "Turbo-Frame" => "true" } | ||
assert_equal @request.flash[:notice], 'Message was successfully created.' | ||
|
||
get messages_path | ||
assert_equal @request.flash[:notice], 'Message was successfully created.' | ||
|
||
get messages_path | ||
assert_nil @request.flash[:notice] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to call Message.create!
? Is the @request
instance variable accessible as request
?
message = Message.create! | |
post messages_path | |
assert_equal @request.flash[:notice], 'Message was successfully created.' | |
get messages_path, headers: { "Turbo-Frame" => "true" } | |
assert_equal @request.flash[:notice], 'Message was successfully created.' | |
get messages_path | |
assert_equal @request.flash[:notice], 'Message was successfully created.' | |
get messages_path | |
assert_nil @request.flash[:notice] | |
post messages_path | |
assert_equal request.flash[:notice], "Message was successfully created." | |
get messages_path, headers: { "Turbo-Frame" => "true" } | |
assert_equal request.flash.notice, "Message was successfully created." | |
get messages_path | |
assert_equal request.flash.notice, "Message was successfully created." | |
get messages_path | |
assert_nil request.flash.notice |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, the message was required in my initial change as I was following the redirect to message_url(id: 1)
.
In the current implementation it isn't required. I think this could use a little clean up.
@jorgemanrubia @brunoprietog are either of you able to enable CI to execute for this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good one! Thanks @p8.
Thanks @jorgemanrubia and @seanpdoyle ! |
Just wondering, but what if a turbo frame response renders the flash messages? Would this not then keep the messages and display them again (potentially) on another subsequent unrelated request? |
A POST request can set the flash and return a redirect response. If a Turbo-Frame request that gets made before the redirect location gets called, the flash gets discared by the Turbo-Frame request.
By making sure a Turbo-Frame request keeps the flash, we can avoid this problem.