# Safe nil in rails views

**URL:** <https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771>\
**Category:** Rails\
**Created:** [September 10, 2010, 10:26am UTC](https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771 "2010-09-10T10:26:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 10, 2010, 10:26am UTC](https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771/1 "2010-09-10T10:26:02Z")

</div>

If you know grails, there is a feature like this: \<% user.name? %\>  
that prevents to chek if the value is null or not.  
In rails view I have to do  
\<% if user.name %\>  
\<%= user.name %\>  
\<% end %\>  
to display user.name if it is not nil.  
There is a shortcut or some feature like that in grails?

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 10, 2010, 10:28am UTC](https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771/2 "2010-09-10T10:28:23Z")

</div>

user.presence || “”

see [http://rubydoc.info/docs/rails/3.0.0/Object:presence](http://rubydoc.info/docs/rails/3.0.0/Object:presence)

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 10, 2010, 10:38am UTC](https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771/3 "2010-09-10T10:38:55Z")

</div>

And if user can potentially be nil, you can also use

user.try(:presence) || “”

This will avoid “undefined method ‘presence’ for nil:NilClass” errors.

On 10 Sep 2010, at 10:27, Christiaan Van den Poel wrote:

> \<% end %\>  
> to display user.name if it is not nil.  
> There is a shortcut or some feature like that in grails?

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 10, 2010, 10:48am UTC](https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771/4 "2010-09-10T10:48:18Z")

</div>

Msan M. wrote:

> If you know grails, there is a feature like this: \<% user.name? %\>  
> that prevents to chek if the value is null or not.  
> In rails view I have to do  
> \<% if user.name %\>  
> \<%= user.name %\>  
> \<% end %\>  
> to display user.name if it is not nil.  
> There is a shortcut or some feature like that in grails?

\<% if user && user.name %\>  
…  
…

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 10, 2010, 11:14am UTC](https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771/5 "2010-09-10T11:14:02Z")

</div>

On 10 September 2010 09:25, Mauro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you know grails, there is a feature like this: \<% user.name? %\>  
> that prevents to chek if the value is null or not.  
> In rails view I have to do  
> \<% if user.name %\>  
> \<%= user.name %\>  
> \<% end %\>  
> to display user.name if it is not nil.  
> There is a shortcut or some feature like that in grails?

\<%= user.name if user.name %\> may be the most concise, or  
\<%= user.name if user && user.name %\> if user may be nil

Colin

---

<div class="post-metadata">

**Author:** ![msan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@msan](https://www.ruby-forum.com/u/msan)\
**Post date:** [September 10, 2010, 10:47am UTC](https://www.ruby-forum.com/t/safe-nil-in-rails-views/194771/6 "2010-09-10T10:47:07Z")

</div>

On 10 September 2010 09:25, Mauro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you know grails, there is a feature like this: \<% user.name? %\>  
> that prevents to chek if the value is null or not.  
> In rails view I have to do  
> \<% if user.name %\>  
> \<%= user.name %\>  
> \<% end %\>  
> to display user.name if it is not nil.

Not a general solution I know, but in such a situation I supply a  
method of User, display\_name, that returns the name or empty string  
(or “Unknown” possibly, dependent on requirement).

Colin
