Pi Horse
Pi Horse

Reputation: 2430

Passing a Ruby variable in a JavaScript function

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>

Upvotes: 0

Views: 263

Answers (1)

Michelle Tilley
Michelle Tilley

Reputation: 159105

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 %>')" >

Upvotes: 2

Related Questions