0

I have the following code in my HTML where I am trying to pass a Ruby variable to the JavaScript function. Can someone please help me with the syntax?

<% @level2.each_with_index do |row2, index2| %> 
................................................
................................................
<a href="javascript:validateUser_com("<%= #{index2} %>")" >Edit</a>

1 Answer 1

2

You're using double quotes twice. For example, if index2 is 1, you end up with the following JavaScript:

<a href="javascript:validateUser_com("1")" >

Since you're using double quotes for the HTML attribute, you should escape the inner quotes or use single quotes. Furthermore, the Ruby expression is not a string, so there's no need for interpolation:

<a href="javascript:validateUser_com('<%= index2 %>')" >
Sign up to request clarification or add additional context in comments.

2 Comments

The html page does not load....it shows some error in the syntax itself....multiple errors around the line....so definitely something wrong with the syntax.
Ah, not sure how I missed this, but what's inside the Ruby expression is not a string--so no need for #{}. I'll update the answer.

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.