hotwired / turbo-rails

Use Turbo in your Ruby on Rails app
https://turbo.hotwired.dev
MIT License
2.13k stars 329 forks source link

Issues with broadcast_to and single table inheritance #673

Open dmbrooking opened 3 months ago

dmbrooking commented 3 months ago

We have a model using STI.

Our base model looks like this:

class Project < ApplicationRecord
  broadcasts_to ->(project) { project}, partial: 'projects/project' // have to explicitly set partial to handle child classes
  ...
end

and one of the child classes is

class Project::Holiday < Project
  ...
end

For this issue, the first line of the partial is <div id="<%= dom_id(project) -%>">. This is where our error is happening.

When we create a new Project, everything works fine. When we create a new Project::Holiday, the broadcast job errors with the following error:

Failure/Error: <div id="<%= dom_id(project_holiday) -%>">

     ActionView::Template::Error:
       undefined local variable or method `project_holiday' for an instance of #<Class:0x000000013e0db058>

How would we handle this to work with STI?

Things we have tried:

  1. Passing in a locals object to the partial. This did not work.
  2. Passing in an object param to broadcasts_to. This did not work.
  3. Adding the broadcasts_to line to project/holiday.rb but this just seemed to be ignored. We confirmed that by removing the partial param on the base class and this errored with Missing partial project/holidays/_holiday with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby]}.

Any ideas?