# Changing text in view with variables

**URL:** <https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430>\
**Category:** Rails\
**Created:** [May 2, 2008, 12:05am UTC](https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430 "2008-05-02T00:05:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![OhMyRuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://www.ruby-forum.com/u/OhMyRuby)\
**Post date:** [May 2, 2008, 12:05am UTC](https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430/1 "2008-05-02T00:05:21Z")

</div>

in my view:

\<% if @main\_singer\_1 == @your\_singer %\>  
\<% if @main\_drummer\_1 == @your\_drummer %\>

i want to have this in a loop so i can change \_1

How would I do this.

I have some idea but I am not sure.

[1…20].each do …  
and then replace \_1 with each of the numbers

or if someone can show the code to do this.

number = “1”

\<% if @main\_singer\_‘number’ == @your\_singer %\>

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://www.ruby-forum.com/u/OhMyRuby)\
**Post date:** [May 2, 2008, 12:12am UTC](https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430/2 "2008-05-02T00:12:26Z")

</div>

On 1 May 2008, at 23:04, OhMyRuby wrote:

> in my view:
> 
> \<% if @main\_singer\_1 == @your\_singer %\>  
> \<% if @main\_drummer\_1 == @your\_drummer %\>
> 
> i want to have this in a loop so i can change \_1

Well you can do this with instance\_variable\_get but it sounds like you  
could make things easier for your self by having arrays @main\_singers  
and @main\_drummers

Fred

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://www.ruby-forum.com/u/OhMyRuby)\
**Post date:** [May 2, 2008, 12:25am UTC](https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430/3 "2008-05-02T00:25:17Z")

</div>

so if number = 1  
you would use \<% if @main\_singer\_instance\_variable\_get(number) ==  
@your\_singer %\> ?

On May 1, 3:06 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://www.ruby-forum.com/u/OhMyRuby)\
**Post date:** [May 2, 2008, 12:43am UTC](https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430/4 "2008-05-02T00:43:27Z")

</div>

No, what am I doing, No it doesn’t work I am way to too tired.

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://www.ruby-forum.com/u/OhMyRuby)\
**Post date:** [May 2, 2008, 12:39am UTC](https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430/5 "2008-05-02T00:39:56Z")

</div>

opps. I got it

\<% if instance\_variable\_get(@main\_singer#{num}) == … %\>

thanks Frederick C.

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://www.ruby-forum.com/u/OhMyRuby)\
**Post date:** [May 2, 2008, 2:43am UTC](https://www.ruby-forum.com/t/changing-text-in-view-with-variables/136430/6 "2008-05-02T02:43:21Z")

</div>

Rather than have your controller create all those individual variables,  
have it pass the singers and drummers in arrays. Then you can loop  
through the array w/code like this:

\<% @main\_singer.each do |singer| %\>  
\<% if singer == @your\_singer then %\>  
blah blah blah  
\<% end %\>  
\<% end %\>

hth,

-Roy
