# If-Else Conditionals in Views

**URL:** <https://www.ruby-forum.com/t/if-else-conditionals-in-views/57283>\
**Category:** Rails\
**Created:** [April 9, 2006, 12:32am UTC](https://www.ruby-forum.com/t/if-else-conditionals-in-views/57283 "2006-04-09T00:32:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 9, 2006, 12:32am UTC](https://www.ruby-forum.com/t/if-else-conditionals-in-views/57283/1 "2006-04-09T00:32:35Z")

</div>

I find myself doing the following quite a lot:

\<% if user %\>\<%= user.name %\>\<% else %\>Anonymous\<% end %\>

I know that if I dont care about an else I can just do:

\<%= user.name if user %\>

Is there any simpler way to handle the else? This is a simple example  
so it’s not really an issue… but for more complicated tests this gets  
quite tedious.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 9, 2006, 12:46am UTC](https://www.ruby-forum.com/t/if-else-conditionals-in-views/57283/2 "2006-04-09T00:46:09Z")

</div>

On Apr 8, 2006, at 3:32 PM, The B. wrote:

> quite tedious.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

```
You can use ternary conditionals.

```

\<%= user ? user.name : “Anonymous” %\>

Cheers-  
-Ezra

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 9, 2006, 1:09am UTC](https://www.ruby-forum.com/t/if-else-conditionals-in-views/57283/3 "2006-04-09T01:09:20Z")

</div>

Brilliant! I didn’t even think of that! I’ve been doing it for ages in  
Java/JSP. Not sure why it didn’t occur to me.

Ezra Z. wrote:

> You can use ternary conditionals.
> 
> \<%= user ? user.name : “Anonymous” %\>

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 9, 2006, 2:37am UTC](https://www.ruby-forum.com/t/if-else-conditionals-in-views/57283/4 "2006-04-09T02:37:10Z")

</div>

The B. wrote:

> Brilliant! I didn’t even think of that! I’ve been doing it for ages in  
> Java/JSP. Not sure why it didn’t occur to me.
> 
> Ezra Z. wrote:
> 
> > You can use ternary conditionals.
> > 
> > \<%= user ? user.name : “Anonymous” %\>

\<%= user.name||“Anonymous” %\>

joey\_\_ now j`ey on IRC

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [April 9, 2006, 3:08am UTC](https://www.ruby-forum.com/t/if-else-conditionals-in-views/57283/5 "2006-04-09T03:08:14Z")

</div>

Hi –

On Sun, 9 Apr 2006, joey\_\_ wrote:

> The B. wrote:
> 
> > Brilliant! I didn’t even think of that! I’ve been doing it for ages in  
> > Java/JSP. Not sure why it didn’t occur to me.
> > 
> > Ezra Z. wrote:
> > 
> > > You can use ternary conditionals.
> > > 
> > > \<%= user ? user.name : “Anonymous” %\>
> 
> \<%= user.name||“Anonymous” %\>

The problem with that is that if user is undefined or nil, it will  
raise an error when it tries to call user.name.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

“Ruby for Rails” coming in PDF April 15, and in paper May 1!

> **[Ruby for Rails](https://www.manning.com/books/ruby-for-rails?new=true&experiment=A)**
>
> NEWER EDITION AVAILABLE
> 
> The Well-Grounded Rubyist, Second Edition is now available. An eBook of the previous edition, The Well-Grounded Rubyist is included at no additional cost when you buy the revised edition! 
> 
> 
> Ruby for Rails helps Rails...
