Forum: Ruby on Rails Instance variables in Javascript param

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
nuubie (Guest)
on 2006-03-02 10:18
How can I make this work ?

<a href="#" onclick="new Effect.Fade(@div_id)"></a>

@div_id is an instance variable ?

TIA
Sam Pohlenz (Guest)
on 2006-03-02 10:30
(Received via mailing list)
nuubie wrote:
> How can I make this work ?
>
> <a href="#" onclick="new Effect.Fade(@div_id)"></a>
>
> @div_id is an instance variable ?
>
> TIA
>

The @div_id needs to be encapsulated in <%= %> tags in order to be
parsed by ruby. i.e.
<a href="#" onclick="new Effect.Fade(<%= @div_id %>)"></a>

It is probably preferable to use the built-in rails helpers for this
though. Try:
<%= link_to_function 'Link text', visual_effect(:fade, @div_id) %>

-Sam
Peter E. (Guest)
on 2006-03-02 10:47
(Received via mailing list)
or use the tag helper:

<%= content_tag "a", "link-text", {:href => "#", :onclick => "new
Effect.Fade(#{div_id})"} %>
Peter E. (Guest)
on 2006-03-02 10:49
(Received via mailing list)
another possible one:

link_to("link-text", {}, {:onclick => "new Effect.Fade(#{div_id})"} )

[did not test that...]
nuubie (Guest)
on 2006-03-02 10:57
Wow, this is a great list.

Real-time response....

Thanks a lot, guys.
Peter E. wrote:
> another possible one:
>
> link_to("link-text", {}, {:onclick => "new Effect.Fade(#{div_id})"} )
>
> [did not test that...]
This topic is locked and can not be replied to.