# Case condition in view

**URL:** <https://www.ruby-forum.com/t/case-condition-in-view/136721>\
**Category:** Rails\
**Created:** [May 5, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721 "2008-05-05T21:47:38Z")\
**Posts on this page:** 8\
**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 5, 2008, 9:47pm UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/1 "2008-05-05T21:47:38Z")

</div>

can you use a case statement in the view.

i get a \_erbout.concat “\n” error.

can someone please show the syntax on this.

---

<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 5, 2008, 10:04pm UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/2 "2008-05-05T22:04:45Z")

</div>

I can do this

case my\_variable  
when “1”, “3” “5”  
…  
when “8”, “9”  
…  
else  
…  
end

but I can not do this in the view.  
Am I missing something here or do I have to use nested IF in the view.

Thanks

---

<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 5, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/3 "2008-05-05T22:28:22Z")

</div>

anyone?

---

<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 5, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/4 "2008-05-05T22:54:21Z")

</div>

How do you use a CASE in the view?

---

<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 6, 2008, 5:15am UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/5 "2008-05-06T05:15:29Z")

</div>

yes, sorry

\<% case my\_variable %\>

tried \<%= case my\_variable %\>

neither one worked.

i will look at the code as to having it in the model or in the  
controller, but i don’t think it would work.

eventually i did if [“1”, “3”, “8”, “15”].any? { |x|…}

and got it to work.

---

<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 6, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/6 "2008-05-06T15:26:12Z")

</div>

Just for reference, you use

\<% %\>

to simply evaluate the code and

\<%= %\>

when you want the evaluation rendered into the view.

Depending on what you’re trying to inject into the view, you could put  
the case in the controller and use it to put the name of a partial in  
an instance variable, then use a render :partial=\>@my\_partial\_view as  
an alternative.

BTW, Phlip was right – you didn’t get quick responses because there  
was not enough info for us to help.

---

<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 6, 2008, 4:38am UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/7 "2008-05-06T04:38:04Z")

</div>

OhMyRuby wrote:

> case my\_variable  
> when “1”, “3” “5”  
> …  
> when “8”, “9”  
> …  
> else  
> …  
> end

That looks fine by itself. (As a style thing, why does my\_variable  
contain  
strings instead of numbers?)

What’s in the … ? Is it %\> \<% ?

If so, write what you need another way (even if eRB supports that  
notation).  
Ideally, you should move the case to a model, and call a method on that  
model here.

Also, does the top case have a \<%= before it?

You must provide more details in questions, to increase the odds someone  
spots  
the actual problem!

–  
Phlip

---

<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 7, 2008, 11:59am UTC](https://www.ruby-forum.com/t/case-condition-in-view/136721/8 "2008-05-07T11:59:57Z")

</div>

OhMyRuby wrote:

> yes, sorry

You _still_ didn’t post enough code!

> neither one worked.

eRB can’t interpret code structured like this. I think you did this:

\<% case %\>  
\<% when %\>  
\<% … %\>  
\<% when %\>  
\<% … %\>  
\<% when %\>  
\<% … %\>  
\<% end %\>

Think of eRB as a way to declare HTML inside strings delimited by %\> \<  
%. This might work:

\<% case  
when  
%\>… \<%  
when  
%\>… \<%  
when  
%\>… \<%  
end %\>

If you don’t see the difference yet, compare to an if statement!

–  
Phlip
