# Visual\_effect

**URL:** <https://www.ruby-forum.com/t/visual-effect/136149>\
**Category:** Rails\
**Created:** [April 29, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/visual-effect/136149 "2008-04-29T12:20:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [April 29, 2008, 12:20pm UTC](https://www.ruby-forum.com/t/visual-effect/136149/1 "2008-04-29T12:20:09Z")

</div>

Hello,

I try to make a visual effect “toggle” on a list generated automatically  
from a controller, I’ve tried this but that doesn’t work? the id  
"answer\_\<%=i%\> isn’t recognized I think?

\<%i=0%\>  
\<removed\_email\_address@domain.invalid do |question|%\>

# \<%= link\_to\_function(question.description) do |page| page.visual\_effect :toggle\_appear, 'answer\_\<%=i%\>' end%\>

\<%=question.answer%\>

\<%i=i+1%\> \<%end%\>

Thank you for your help,

---

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [April 29, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/visual-effect/136149/2 "2008-04-29T12:29:36Z")

</div>

On 29 Apr 2008, at 11:20, san 1981 wrote:

> # \<%= link\_to\_function(question.description) do |page| page.visual\_effect :toggle\_appear, 'answer\_\<%=i%\>' end%\>

When you call page.visual\_effect you’re not in erb-land, so you need  
“answer\_#{i}”

Fred

---

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [April 29, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/visual-effect/136149/3 "2008-04-29T14:54:31Z")

</div>

thank you Fred,

I’ve changed so this in my code but that works only for the first title;  
if I click on another title, it’s always the description of the first  
title that disappears/appears? I would like to make a  
appearition/disappearition of the description linked to the title  
concerned.

\<%i=0%\>  
\<removed\_email\_address@domain.invalid do |question|%\>

# \<%= link\_to\_function(question.description) do |page| page.visual\_effect :toggle\_appear, 'answer\_#{i}' end%\>

\<%=question.answer%\>

 \<%i=i+1%\>

---

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [April 29, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/visual-effect/136149/4 "2008-04-29T15:45:02Z")

</div>

Hi,

On Apr 29, 1:54 pm, san 1981 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<%i=i+1%\>

You’ve nearly got it, but you’re confusing ERB scope and ruby scope.  
Also strings surrounded with single quotes in ruby don’t get  
interpolated, you need double quotes.

Finally you don’t need your index variable i when you could use  
each\_with\_index instead.

\<removed\_email\_address@domain.invalid\_with\_index do |question, i| %\>

# \<%= link\_to\_function(question.description) do |page| page.visual\_effect :toggle\_appear, “answer\_#{i}” end %\>

```
<p id="answer_<%=i%>">
  <%=h question.answer%>
</p>

```

\<% end %\>

---

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [April 29, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/visual-effect/136149/5 "2008-04-29T19:11:22Z")

</div>

On Tue, Apr 29, 2008 at 6:54 AM, san 1981  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> thank you Fred,
> 
> I’ve changed so this in my code but that works only for the first title;  
> if I click on another title, it’s always the description of the first  
> title that disappears/appears? I would like to make a  
> appearition/disappearition of the description linked to the title  
> concerned.

check the source to make sure it looks as you’d expect it to.

---

<div class="post-metadata">

**Author:** ![san1981](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ad7895/32.png) [@san1981](https://www.ruby-forum.com/u/san1981)\
**Post date:** [April 29, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/visual-effect/136149/6 "2008-04-29T16:05:38Z")

</div>

This could arguably be simplified even further with the  
content\_tag\_for and dom\_id methods. Something like this (untested):

\<% @questions.each do |question| %\>  
\<% content\_tag\_for :h1, question do  
link\_to\_function(question.description) do |page|  
page.visual\_effect :toggle\_appear, dom\_id(question, “answer”)  
end  
end %\>

```
 <% content_tag_for :p, question, "answer" do %>
   <%=h question.answer%>
 <% end %>

```

\<% end %\>

Search the Rails API docs for dom\_id and content\_tag\_for.
