Skip to content

Dashboard card hydration issue #4073

@maokomioko

Description

@maokomioko

Dashboard card hydration regression

Summary

Requests hitting Avo::Dashboards::CardsController#show can raise NoMethodError: undefined method hydrate' for nilafter the first dashboard render. The controller callscard.hydrateon the result ofparent.item_at_index(params[:index]), but the lookup returns nil` because card indices drift between requests.

Root cause

  1. Avo::Dashboards::BaseDashboard mixes in Avo::Cards::Concerns::HasCards, which tracks a card_index counter and a cards_holder array.
  2. In Avo 3.24.x the concern stores card_index as a class_attribute. The counter therefore retains its value across instances and requests.
  3. Every time the DSL method card is invoked, card_index is incremented.
  4. Rendering the dashboard page (first request) builds the card list and leaves the counter at the total number of items (e.g. 4).
  5. Each subsequent hydration request (triggered by the dashboard fetching /cards/:id) also calls fetch_cards. Because the counter was never reset, the first card invocation in that request assigns index 4 instead of 0. As a result item_at_index(0) returns nil, leading to the crash.

Reproduction steps

  1. Upgrade an app to Avo 3.24.x (the issue is tied to the class_attribute change in HasCards).
  2. Define a dashboard with at least one card in def cards.
  3. Load the dashboard in the browser.
  4. Observe that the initial render succeeds, but XHR calls to /admin/dashboards/:dashboard/cards/:card_id fail with NoMethodError: undefined method hydrate' for nil`.

Suggested fix

Reset both the card_index class attribute and the instance @cards_holder before repopulating cards. This keeps indices aligned with the positions expected by the hydration controller.

class Avo::Dashboards::Base < Avo::Dashboards::BaseDashboard
  def fetch_cards
    self.class.card_index = 0
    @cards_holder = []

    super
  end
end

Impacted versions

  • Confirmed on Avo 3.24.1 (avo-dashboards 3.24.1).
  • Not reproducible on earlier releases where card_index was stored per instance.

Related production issue

Metadata

Metadata

Assignees

Labels

Waiting on ReproductionPending reproduction repository or detailed reproduction steps to proceed with issue resolution.

Type

No type

Projects

Status

To Do

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions