hardow2011
hardow2011

Reputation: 59

Why does Rails redeclare js variable with link_to, but not on reload?

I have a Rails 7 app with Ruby 3.

I have the following in my index.html.erb

<script>
    const chart_data = <%= sanitize @transactions %>;
</script>

If I access index.html.erb from a link_to tag, the console says:

Uncaught SyntaxError: redeclaration of const chart_data

But if I reload the page, it works fine.

Why does reloading the page fix the js variable redeclaration issue?

Upvotes: 2

Views: 62

Answers (1)

Akanksha
Akanksha

Reputation: 190

I had the same issue. I solved it by adding data: {turbo: false} to the "link_to" tag.

This reloads the page instead of loading it from cache which functions same as you refreshing the page.

This may not be the best solution for everyone, but I was facing this issue on just one page and with just one JS file, so this is a faster solution than changing my JS file (I also do not know what changes need to be made in JS to fix the issue.)

Upvotes: 0

Related Questions