Prevent URL changing when using onChange and Turbo.visit

I have 2 selects where changes to the first select refresh the second select.

I am using onChange on the first select with Turbo.visit to trigger a re-rendering:

onChange:"Turbo.visit(`/sites/index_by_country?country_id=${this.value}`)"

This calls a controller method that renders a turbo_stream, updating the turbo_frame_tag around the second select.

def index_by_country
  ...
  render turbo_stream: turbo_stream.update(...)
end

2022-03-28_20-04-53

This works nicely, the second select is refreshed, however when I do this, the ‘Turbo.visit’ changes the URL.
What can I do to not change the URL?
I want it to remain the same, no matter how often I change the first select.

I did not find a solution to using Turbo.visit directly in the onchange.

Instead I am using a Stimulus controller and request.js as shown in this GoRails video: Dynamic Select Fields in Rails using Hotwire - YouTube

1 Like

You could use turbo-frame if you didn’t want to change URL