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?

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

Sign up to request clarification or add additional context in comments.

Comments

4

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

"get_maps('#{@location_string}');"

3 Comments

I don't think this is it. I changed it and the get_maps doesn't seem to be firing. I know get_maps works because when I put onclick: 'get_maps("San Francisco");' it does the right thing.
Ah I think you need single quotes around your #{@location_string} to make your code work. Change and I'll give you credit :)
Done. Was assuming your @location_string already had the quotes.
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

Comments

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.