Turbo Frames and Streams Implementation
Overview
Section titled “Overview”This implementation provides a granular approach for Turbo functionality using Turbo Frames and Turbo Streams, allowing specific sections of pages to have Turbo functionality while keeping the rest of the page (including menus and legacy functionality) untouched.
Problem Solved
Section titled “Problem Solved”Enabling Turbo Drive at the page level was breaking functionality across the app, particularly:
- CRM menu panel no longer loading
- Interference with existing form interactions
- Breaking existing AJAX functionality
- Affecting all elements on the page, not just the specific functionality that needed Turbo
Solution
Section titled “Solution”1. Default State: Turbo Drive Disabled
Section titled “1. Default State: Turbo Drive Disabled”Turbo Drive is disabled by default in client/js/crm/crm.index.js:
// Disable Turbo Drive by default - we'll use Turbo Frames and Streams for specific interactionsTurbo.session.drive = false;2. Section-Level Turbo Functionality
Section titled “2. Section-Level Turbo Functionality”Turbo functionality is enabled for specific sections via:
- Controller Action: Add
before_action :enable_turbo_frames, only: [:index]to controllers - View Helper: Use
turbo_section_wrapperhelper to wrap sections that need Turbo functionality - HTML Attribute: The wrapper adds
data-turbo="true"to the section when@turbo_frames_enabledis set - JavaScript Detection: The JavaScript enables Turbo functionality only for sections with the
data-turboattribute
3. Implementation Details
Section titled “3. Implementation Details”ApplicationController Method
Section titled “ApplicationController Method”# Enable Turbo Frames and Streams for specific sectionsdef enable_turbo_frames @turbo_frames_enabled = trueendView Helper
Section titled “View Helper”# Enable Turbo functionality for specific sections without affecting the entire pagedef turbo_section_wrapper(id: nil, class_names: nil, &block) turbo_attrs = {} turbo_attrs[:id] = id if id turbo_attrs[:class] = class_names if class_names
if @turbo_frames_enabled turbo_attrs[:'data-turbo'] = 'true' end
content_tag(:div, turbo_attrs, &block)endView Usage
Section titled “View Usage”<%= turbo_section_wrapper(id: 'videos-turbo-section', class_names: 'videos-turbo-section') do %> <%= render 'filter_form', q: @q %> <%= render 'videos' %><% end %>JavaScript Logic
Section titled “JavaScript Logic”// Enable Turbo functionality for specific sectionsdocument.addEventListener('DOMContentLoaded', () => { const turboSections = document.querySelectorAll('[data-turbo="true"]'); turboSections.forEach(section => { console.log('🔧 Turbo enabled for section:', section.id || section.className);
// Enable Turbo Frames within this section const turboFrames = section.querySelectorAll('turbo-frame'); turboFrames.forEach(frame => { console.log('🔧 Turbo Frame found:', frame.id); });
// Enable Turbo Streams for forms within this section const turboStreamForms = section.querySelectorAll('form[data-turbo-stream]'); turboStreamForms.forEach(form => { console.log('🔧 Turbo Stream form found:', form.action); }); });});Enabling Turbo Frames on a Controller
Section titled “Enabling Turbo Frames on a Controller”To enable Turbo Frames for specific actions in a controller:
class MyController < CrmController before_action :enable_turbo_frames, only: [:index, :show]
def index # Turbo Frames will be enabled for this action endendUsing Turbo Section Wrapper in Views
Section titled “Using Turbo Section Wrapper in Views”Wrap sections that need Turbo functionality:
<%= turbo_section_wrapper(id: 'my-turbo-section', class_names: 'my-turbo-section') do %> <%= render 'my_form' %> <%= render 'my_results' %><% end %>Currently Enabled Controllers
Section titled “Currently Enabled Controllers”VideosController-indexaction (with Turbo Frames for filter form and results)
Benefits
Section titled “Benefits”- Granular Control: Only specific sections have Turbo functionality, leaving the rest of the page untouched
- Menu Compatibility: CRM menu and other page elements work normally
- Gradual Migration: Can be enabled on more sections as needed
- Debugging: Clear logging shows which sections have Turbo enabled
- Performance: Only necessary sections use Turbo, improving overall performance
- Visual Feedback: Development styling helps identify Turbo-enabled sections
Testing
Section titled “Testing”To test if Turbo functionality is working on a specific section:
- Check browser console for ”🔧 Turbo enabled for section” messages
- Verify the section has
data-turbo="true"attribute - Look for the blue dashed border around Turbo-enabled sections (development only)
- Test form submissions and navigation within the Turbo-enabled section
- Verify that the menu and other page elements work normally
Future Considerations
Section titled “Future Considerations”- Monitor performance impact on enabled sections
- Consider enabling on more sections as needed
- May need to add more Turbo Stream support for complex forms
- Consider adding Turbo Frame support for specific components
- Remove development styling in production
- Add more granular control options for different types of Turbo functionality