0

I have an array of video names, I want to grab a random video name from this array. But when I call a helper method to do all this, I get the same random number, and random video each time.

I believe this is because the helper method is only called once even though I'm trying to "call it" multiple times, it's just the results of that method that are "called" multiple times.

So what I'm thinking is to find a way to send a JavaScript variable to the helper method, but I have no idea (nor does Google) of how best to do this.

To keep it simple, with just trying to obtain a new random number each time:

JS:

function randomNumber() {
    alert("<%= random_number %>");
}

setTimeout(function(){
    randomNumber();
}, 2000);

html.erb:

helper_method :random_number
def random_number
  rand(0..10)
end

The same random number is shown each time.

Riveascore
  • 1,724
  • 4
  • 26
  • 46
  • Please share your helper method code. The problem most likely lies there. – Gjaldon Dec 25 '13 at 19:41
  • 3
    Ruby is a server side language. Meaning it will not continue to execute once the information has been sent to the client. There are ways around this. Most notably, AJAX. In this simple case though. You would probably be better off creating a random number in JavaScript. – Justin Wood Dec 25 '13 at 20:03
  • The thing is, I need to send the random number to the helper method, so it can grab a random video name from the array of names. – Riveascore Dec 25 '13 at 20:04
  • As @JustinWood has said there is no way of passing a javascript variable to ruby code in script. You have to do it by sending a ajax request. – abhas Dec 25 '13 at 21:22
  • U can do one thing, call a js function and pass number of videos at that time, and use that number to generate a random number and after that send this random number with request. Like currently I have 20 videos then I will use `random` function on 20 and it generates a number 14 for example, then I will send 14 to controller with my request and then I will do whatever I want. :) – Gopal S Rathore Dec 26 '13 at 05:12

1 Answers1

0

Requests

Rails works on "requests"

Because Rails is server-side, it works with these requests to "render" the pages / views you see on your screen. Each request is treated individually, meaning that you have to pass it everything the server will need to render the correct view for you

The problem you're alluding to is you're trying to use data which has already been rendered. The "helper" view was rendered when you sent the HTTP request to Rails, and consequently is going to use the same data over and over


Client-Side Vs Server-Side

The problems you're seeing come from the difference between client-side (JS) and server-side (Rails). The difference is that you can't access Rails from JS, unless you send a "request" (probably via ajax)

In order to trigger Rails from the client-side, you have to send a request. That's either done with a browser click, or Ajax


Your Question

You're trying to load a random number between 1 & 10 from Rails

This can be handled entirely by JS if you use the right functions / code

Alternatively, you can use Ajax to request the helper from Rails, but this is highly inefficient, as it will send an unnecessary request to the server

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147