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.
E80dbf9c0c4871405316c452e2c3531b?d=identicon&s=25 nuubie (Guest)
on 2006-03-02 09:18
How can I make this work ?

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

@div_id is an instance variable ?

TIA
C77b15d1fd19b42984c35cfe3260a1d5?d=identicon&s=25 Sam Pohlenz (Guest)
on 2006-03-02 09: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
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 Peter Ertl (Guest)
on 2006-03-02 09:47
(Received via mailing list)
or use the tag helper:

<%= content_tag "a", "link-text", {:href => "#", :onclick => "new
Effect.Fade(#{div_id})"} %>
36958dd94ca666a38483df282a5214d5?d=identicon&s=25 Peter Ertl (Guest)
on 2006-03-02 09:49
(Received via mailing list)
another possible one:

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

[did not test that...]
E80dbf9c0c4871405316c452e2c3531b?d=identicon&s=25 nuubie (Guest)
on 2006-03-02 09:57
Wow, this is a great list.

Real-time response....

Thanks a lot, guys.
Peter Ertl 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.