0

I have a html page with a script, It looks like

<html>
</html>

<script>
<script>

The page needs to run this script to display the content correctly.

But when I redirect to this page with following code:

redirect_to user_trips_path(params[:user_id])

I noticed that it will not refresh the page. If I refresh the page manually it will display the content correctly.

My question is how to make this automatic?

I try to google this question.

  • You probably have Turbo enabled. You may read this thread. https://stackoverflow.com/questions/73359149/how-to-load-page-specific-custom-javascript-functions-in-a-rails-7-app-with-turb/73362166#73362166 Also inline JS through – Maxence Nov 13 '22 at 23:15
  • In addition to what @Maxence is saying that HTML is broken - you're missing a `` end tag. If this is the actual code thats running it will create a syntax error when the JS intrepreter runs into ` – max Nov 14 '22 at 10:06

2 Answers2

0

There are unlimited ways to refresh a page - but some helpful aspect of your situation:

1- html tag: <meta http-equiv="refresh" content="30">

w3schools link

put it on your html or inject it with JS after page load.

2- JavaScript or jQuery:

How do I refresh a page using JavaScript?

3- if you have View on your Rails app you can create a route and method for run sample_name.js.erb and then in this file with some JS code you can open your partial and main .html.erb file

--

If after the first time you want to run a script to start to refresh the page you can redirect users with special param in user_trips_path and then in your JS code check if its present then run your code.

0

In Rails 7

If you want to disable Turbo Drive you can set Turbo.session.drive = false in your javascript/application.js file.

   import { Turbo } from "@hotwired/turbo-rails"
   Turbo.session.drive = false

=> It will reload the page while navigating

LihnNguyen
  • 632
  • 4
  • 10