'Rails: turbo-frame with src requested interpreted as format.html

I've noticed this strange behavior of turbo frames loading lazily via src attribute setting:

nav
  = turbo_frame_tag 'primary_menu', src: primary_menu_path
  : def primary_menu
  :   respond_to do |format|
  :     format.turbo_stream
> :     format.html { binding.pry ; render plain: 'should not reach this block' }
  :   end
  : end

)> turbo_frame_request?
=> true

For some reason, it is interpreted as regular requested html request, although turbo_frame_request? returns => true. What's causing it and how to fix it?

P.S. primary_menu.turbo_stream.slim exists for that controller action.

Local versions:

hotwire-rails 0.1.3 @hotwired/turbo-rails 7.0.0-beta.5



Solution 1:[1]

Before someone with better knowledge of turbo internals explains it properly, below is a method that worked for me:

<%= turbo_frame_tag 'primary_menu', src: primary_menu_path %>

Would be interpreted as HTML request:

def primary_menu
  # load your thing

  respond_to do |format|
    format.html
  end
end

in your primary_menu.html.erb make sure you wrap your content with <%= turbo_frame_tag 'primary_menu' do %> block.

Keep in mind that:

  • turbo_frame_tag names have to match here and its original definition/require.
  • any additional turbo_frame_tag wrapped blocks in that response would be silently ignored.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nikita Fedyashev