Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rails variable into onclick function call

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?

like image 252
locoboy Avatar asked Dec 20 '22 14:12

locoboy


1 Answers

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 ''.

like image 78
panta Avatar answered Jan 12 '23 00:01

panta