scientiffic
scientiffic

Reputation: 9415

Pass Javascript variable to Rails path

Is there a way to pass a Javascript variable to a Rails path?

For example, I have in my html.erb file:

<script>
for (i=1, i< numSteps, i++){
   var step = i
   td.innerHTML = <%= project_step_path(@project, step) %>
}
</script>

But this doesn't work since step is a Javascript variable.

Upvotes: 0

Views: 377

Answers (2)

Quentin
Quentin

Reputation: 944432

In this case, the Ruby will run on the server. It will generate some text. That text is sent to the browser. The browser will parse it as HTML and JS.

You can't do bi-directional synchronous communication in a single HTTP request/response.

So no.

Each time you want to send data from client side JavaScript to server side Ruby you need to issue a new HTTP request.

Issuing such a request can be done by:

  • setting location
  • submitting a form
  • using the XMLHttpRequest object
  • generating an element (such as a script or an image) that can have an external src
  • and a host of other methods.

Upvotes: 3

epascarello
epascarello

Reputation: 207557

  • JavaScript runs on the browser
  • Rails runs on the server.

They can not run together.

Upvotes: 2

Related Questions