Fix layout rendering for turbo_streams

Closes #22
This commit is contained in:
cryptogopher 2024-02-13 20:48:00 +01:00
parent 8b81ae2dc5
commit 3a3ae71320
4 changed files with 14 additions and 12 deletions

View File

@ -1,4 +1,11 @@
class ApplicationController < ActionController::Base 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? helper_method :current_user_disguised?
before_action :authenticate_user! before_action :authenticate_user!
@ -47,8 +54,6 @@ class ApplicationController < ActionController::Base
def run_and_render(action) def run_and_render(action)
send action send action
# 2024-01-17, Rails 7.1.2: For unknown reason turbo_stream layout is omitted render action
# during render on POST method only (GET, DESTROY are ok).
render action, layout: 'application'
end end
end end

View File

@ -1,5 +1,4 @@
<%# TODO: make sure turbo_stream layout is used in new/edit %> <%= turbo_stream.replace :unit_form do %>
<%= turbo_stream.update :unit_form_frame do %>
<%= form_with model: @unit, html: {id: :unit_form} do %> <%= form_with model: @unit, html: {id: :unit_form} do %>
<% end %> <% end %>
<% end %> <% end %>

View File

@ -1,13 +1,11 @@
<div class="rightside"> <div class="rightside">
<% if current_user.at_least(:active) %> <% if current_user.at_least(:active) %>
<%= turbo_frame_tag do %> <%= image_link_to t('.add_unit'), 'plus-outline', new_unit_path, id: :add_unit,
<%= image_link_to t('.add_unit'), 'plus-outline', new_unit_path, id: :add_unit, onclick: 'this.blur();', data: {turbo_stream: true} %>
onclick: 'this.blur();', data: {turbo_stream: true} %>
<% end %>
<% end %> <% end %>
</div> </div>
<%= turbo_frame_tag 'unit_form_frame' %> <%= tag.div id: :unit_form %>
<table class="main items"> <table class="main items">
<thead> <thead>
@ -20,7 +18,7 @@
<% end %> <% end %>
</tr> </tr>
</thead> </thead>
<tbody id="units" is="turbo-frame"> <tbody id="units">
<%= render(@units) || render_no_items %> <%= render(@units) || render_no_items %>
</tbody> </tbody>
</table> </table>

View File

@ -1,7 +1,7 @@
<% link_id = dom_id(@unit.base || @unit, :add) %> <% link_id = dom_id(@unit.base || @unit, :add) %>
<%= turbo_stream.disable link_id -%> <%= 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 %> <%= form_with model: @unit, html: {id: :unit_form} do %>
<% end %> <% end %>
<% end %> <% end %>