2

I am trying to use a JavaScript variable in an HTML erb, using <% %> tags. query[i] is something I have in loop I am trying to call:

 <%= Table.update('query[i]')%>; 

How can I use query[i] from JavaScript for the actual value?

0

1 Answer 1

1

You can't do this. erb runs on the server to generate the HTML, which is then sent to the client, where the Javascript runs. By the time the Javascript variable is available, erb has already executed, and we're not even on the same machine anymore!

It looks as though you want Javascript code to supply a value which is then used as an argument to a database call. You'll have to do this via a post-back; i.e., the Javascript can make an AJAX call, a new erb could generate the new HTML fragment, and then the Javascript can receive it and plug it into the page.

Here's the first link I got when I googled "erb AJAX".

Sign up to request clarification or add additional context in comments.

8 Comments

Can i set a separate ruby variable with the javascript variable and then use that variable each time in the loop within the <% %> tags?
I don't think you read my answer carefully enough. When the Javascript code actually runs, the erb process has already completed, and you're on a completely separate machine. erb runs in your web server, and Javascript runs on the user's desktop. While erb is running, the Javascript variable doesn't exist yet.
@ErnestFriedman-Hill: Man, where do they get these ideas from? :-) stackoverflow.com/questions/8683059/…
Thank you for your reply.. Can you give me an example?
@SergeiTulentsev .. i don't think this questions is as silly as the one you've posted.. newbies surely have some doubts. Everyone is not intellectual enough.. Thats y there are these forums...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.