Tutor HuntQuestions

Tutor Hunt Questions

Can i use javascri pt variable inside erb tag ?
7 years ago

Ruby On Rails Question asked by Fakhir

Know the Answer?

Please enter your response to the question below. The student will get a notification as soon your response has been approved by our moderation team.

Submit Answer

1 Answer


It depends on what you are doing. If you want to execute Javascri pt in erb it can be done simply by putting

<scri pt>
var foo
//Do something with foo
<scri pt/> inside your erb template.

However, if you want to pass a variable from one file to another you will need to use something called Ajax. Erb is a templating engine (simply turns ruby to html) and this happens on the server. Html and JS are run on client side (also known as your browser). Therefore, because erb is rendered on the server side first you will need to call back to your server once the html and JS have run in your browser (client side). This is done using Ajax.
Look up railscasts on Ajax!

If you just want to know what the erb tag for JS is, you can use something like: javascri pt_tag "alert(`This is a test`)".

Hope this helps a little. Without more information I can`t be more specific!

All the best.
Answered by [Deleted Member]
    Rate Up  Rate Down