question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rails, Turbolinks, Stimulus and asset pipeline

See original GitHub issue

What’s the best way of installing Stimulus with Rails, Turbolinks using the asset pipeline?

I’m having an issue with the Stimulus action function firing multiple times after I visit another page and then come back to the original page with the Stimulus controller.

I have installed Stimulus by just downloading the https://unpkg.com/stimulus/dist/stimulus.umd.js file and then pulling it into application.coffee using #= require stimulus.umd

I was thinking I could call @application = Stimulus.Application.start() inside application.coffee file but then it is run on every page visit.

Should @application = Stimulus.Application.start() and @application.register "filter", class extends Stimulus.Controller be called from within a Turbolinks event? I tried a few different events but each time the action function would be called multiple times after revisiting the page.

Here is my current setup:

application.coffee

#= require jquery
#= require jquery_ujs
#= require turbolinks
#= require stimulus.umd
#= require_self
#= require filter

@application = Stimulus.Application.start()

filter.coffee

@application.register "filter", class extends Stimulus.Controller
  update: ->
    # Code should only be called once each time the action triggered.
    # After visiting another Turbolinks page and coming back this is called multiple times each time the action is triggered.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sstephensoncommented, Feb 2, 2018

Closing this for now, but feel free to continue the conversation over on the new Stimulus Discourse.

1reaction
scottharveycommented, Feb 2, 2018

@javan I created an example app and it seems to be working as expected so there is probably something else in my main app that’s causing the events to fire twice.

One thing I did notice is that the Stimulus controller is initialized twice when I visit the page using a Turbolinks link. I don’t think it’s causing an issue but just something that was interesting, here’s a gif showing that.

screen

If I just refresh the page with the Stimulus controller then the controller is only initialized once.

Here’s a link to my example app https://github.com/scottharvey/stimulus_asset_pipeline

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rails, Turbolinks, Stimulus and asset pipeline #88 - GitHub
What's the best way of installing Stimulus with Rails, Turbolinks using the asset pipeline? I'm having an issue with the Stimulus action ...
Read more >
Installing Turbo | The Pragmatic Programmers - Medium
If Webpacker is not installed, the gem installs Turbo and Stimulus using the asset pipeline, including the Stimulus auto-load behavior we'll ...
Read more >
How To Add Stimulus to a Ruby on Rails Application
In this tutorial, you will install and use Stimulus to build on an existing Rails application that offers readers information about sharks.
Read more >
Rails 7 will have three great answers to JavaScript in 2021+
With Rails 7, we're replacing Webpacker, Turbolinks, UJS with import maps plus Turbo and Stimulus from Hotwire as the defaults.
Read more >
Installing Turbo with the Asset Pipeline - Hotwire Discussion
But now I don't know how to expose the Turbo object globally like it used to be with Turbolinks. mrhead January 5, 2021,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found