4

Hi I have the following:

 = link_to @place.name, '#', onclick: 'get_maps(#{@location_string});'

I'm currently trying to link to a javascript call with a rails variable argument but for some reason the rails variable @location_string isn't being passed to the javascript call with the variable in it. Currently it's being rendered as a string:

<a href="#" onclick="get_maps(#{@location_string});">3308 Kanaina Ave</a>

Any thoughts on how to solve this?

4

3 に答える 3

5

You need to put:

= link_to @place.name, '#', :onclick => "get_maps('#{@location_string}');"

The onclick is a parameter of link_to.
And the parameter inside get_maps should go inside ''.

于 2012-11-03T19:41:08.530 に答える
4

The #{} syntax doesn't work inside single quotes. You want

"get_maps('#{@location_string}');"
于 2012-11-03T19:05:38.630 に答える
2

People say that using inline JS, such as onclick='', onmouseover etc. is bad. You can pass ruby variable via data attr of link and attach event using jquery click() function

于 2012-11-03T21:12:31.307 に答える