0

I have this code:

<% @hello = "hello world" %>
<td class="text"><%= link_to project.name, project,
                  :onmouseout=>"hidetooltip()",
                  :onmouseover=>"showDescription()"%>
</td>

The showDescription JavaScript function takes a string parameter. I want to pass @hello as a parameter to showDescription. I tried everything and can't seem to get it to work.

2 Answers 2

2

Here you go:

<% @hello = "hello world" %>
<%= link_to project.name, project, :onmouseout=>"hidetooltip()",
                                   :onmouseover=>"showDescription(#{ @hello.to_json })"%>
Sign up to request clarification or add additional context in comments.

6 Comments

this works? a comma or a to_json seems to be missing. (btw, why the non-idiomatic spaces in { }?)
@tokland: it's just text, so no need to use to_json
To be clear, in my comment above I meant of course a quote ('), not a comma. I still don't see how showDescription(hello world) may be valid JS.
I have some experience with this, I've fallen myself in this same bug on uncountable occasions :-) I have no strong opinion against '#{var}', but my advice is var.to_json (even with integers), so you don't have to think for a second about the types. Let to_json make the Ruby value->JS code conversion. Also, imagine @hello has a quote inside, boom.
@tokland: you're definitely right, it's the best way to proceed
|
0

another way to do that is using gem client variable. It helps you to do it easily

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.