From 3a3ae71320949d75f149aee3f375e86b35d387c1 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Tue, 13 Feb 2024 20:48:00 +0100 Subject: [PATCH] Fix layout rendering for turbo_streams Closes #22 --- app/controllers/application_controller.rb | 11 ++++++++--- app/views/units/edit.turbo_stream.erb | 3 +-- app/views/units/index.html.erb | 10 ++++------ app/views/units/new.turbo_stream.erb | 2 +- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 11cd51e..6956a31 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,11 @@ class ApplicationController < ActionController::Base + # Turbo-rails disables layout rendering for turbo_frame requests (i.e. + # requests that specify 'turbo-frame:' header). + # As a side effect, this also disables layout for turbo_stream requests that + # happen to originate from within turbo frame (e.g. turbo_frame_tag or tag + # with 'is="turbo-frame"' attribute). To fix this, either frame tags must not be + # used, or custom layout method needs to be defined. + helper_method :current_user_disguised? before_action :authenticate_user! @@ -47,8 +54,6 @@ class ApplicationController < ActionController::Base def run_and_render(action) send action - # 2024-01-17, Rails 7.1.2: For unknown reason turbo_stream layout is omitted - # during render on POST method only (GET, DESTROY are ok). - render action, layout: 'application' + render action end end diff --git a/app/views/units/edit.turbo_stream.erb b/app/views/units/edit.turbo_stream.erb index b796499..d3c8bee 100644 --- a/app/views/units/edit.turbo_stream.erb +++ b/app/views/units/edit.turbo_stream.erb @@ -1,5 +1,4 @@ -<%# TODO: make sure turbo_stream layout is used in new/edit %> -<%= turbo_stream.update :unit_form_frame do %> +<%= turbo_stream.replace :unit_form do %> <%= form_with model: @unit, html: {id: :unit_form} do %> <% end %> <% end %> diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index 143aac8..5ebd3e6 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -1,13 +1,11 @@
<% if current_user.at_least(:active) %> - <%= turbo_frame_tag do %> - <%= image_link_to t('.add_unit'), 'plus-outline', new_unit_path, id: :add_unit, - onclick: 'this.blur();', data: {turbo_stream: true} %> - <% end %> + <%= image_link_to t('.add_unit'), 'plus-outline', new_unit_path, id: :add_unit, + onclick: 'this.blur();', data: {turbo_stream: true} %> <% end %>
-<%= turbo_frame_tag 'unit_form_frame' %> +<%= tag.div id: :unit_form %> @@ -20,7 +18,7 @@ <% end %> - + <%= render(@units) || render_no_items %>
diff --git a/app/views/units/new.turbo_stream.erb b/app/views/units/new.turbo_stream.erb index 0c7b0d2..a11eb9e 100644 --- a/app/views/units/new.turbo_stream.erb +++ b/app/views/units/new.turbo_stream.erb @@ -1,7 +1,7 @@ <% link_id = dom_id(@unit.base || @unit, :add) %> <%= turbo_stream.disable link_id -%> -<%= turbo_stream.update :unit_form_frame do %> +<%= turbo_stream.replace :unit_form do %> <%= form_with model: @unit, html: {id: :unit_form} do %> <% end %> <% end %>