Skip to content
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

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/turbo/frames/frame_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Turbo::Frames::FrameRequest
included do
layout -> { "turbo_rails/frame" if turbo_frame_request? }
etag { :frame if turbo_frame_request? }
before_action { flash.keep if turbo_frame_request? }

helper_method :turbo_frame_request?, :turbo_frame_request_id
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def section

def create
respond_to do |format|
format.html { redirect_to message_url(id: 1) }
format.html { redirect_to message_url(id: 1), notice: "Message was successfully created." }
format.turbo_stream { render turbo_stream: turbo_stream.append(:messages, "message_1"), status: :created }
end
end
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</head>

<body class="<%= "turbo-native" if turbo_native_app? %>">
<%= flash[:notice] %>
<%= yield %>
</body>
</html>
16 changes: 16 additions & 0 deletions test/frames/frame_request_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ class Turbo::FrameRequestControllerTest < ActionDispatch::IntegrationTest
assert_not_equal etag_with_frame, etag_without_frame
end

test "frame requests keep the flash" do
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]
Comment on lines +39 to +51
Copy link
Contributor

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?

Suggested change
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

Copy link
Contributor Author

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.

end

test "turbo_frame_request_id returns the Turbo-Frame header value" do
turbo_frame_request_id = "test_frame_id"

Expand Down
Loading