New Discussion

Notifications

You’re not receiving notifications from this thread.

Problem with Stimulus and counter_culture gem (counter_cache)

0
Rails

Hy all, i have a problem with the counter_culture gem with my stimulus controller.
I have installed the gem and configured all.
I have 3 model :
Thats working fine for this relation: Category have a capsules_count column for counting how many capsules have a category

But i have a problem for counting the number of wishlist each Capsule have:

THE counter increase well in backend but in my frontend ( with stimulus ) i need to refresh the page for having the good value:

Here my models / controller and views !

CAPSULE

class Capsule < ApplicationRecord
    extend FriendlyId
    friendly_id :name, use: :slugged

    monetize :price_cents, allow_nil: true
    geocoded_by :address
    after_validation :geocode

    ACCEPTED_CONTENT_TYPES = %w[image/png image/jpeg].freeze

    has_many_attached :images

    belongs_to :user

    belongs_to :category
    counter_culture :category, touch: true

    has_many :wishlists, dependent: :destroy
    has_many :wishlisted_users, through: :wishlists, source: :user, dependent: :destroy
end

WISHLIST

class Wishlist < ApplicationRecord
  belongs_to :user
  belongs_to :capsule
  counter_culture :capsule
end

THE FRONT END

<div class="bg-white flex justify-center items-center rounded-md text-xs p-1" data-controller="wishlist-counter" data-capwishlists="<%= capsule.wishlists_count %>">
        <span data-wishlist-counter-target="text"></span>
</div>

THE STIMULUS CONTROLLER

import {Controller} from "@hotwired/stimulus"

// Connects to data-controller="wishlist-counter"
export default class extends Controller {
  static targets = ['counter', 'text'];
  connect() {
    this.textTarget.innerText = this.element.dataset.capwishlists;
  }
}

When i click in the like button the value of my wishlists_count upgrade well but i need to refresh the page for stimulus in my front end to show the exact value and i dont know why ^ ^ ?

THE wishlists_count add + 1 value well in back end but the value is not INSTANTLY refreshed in my front end (stimulus) ...

Thanks you for anyone can help me.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 91,345+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.