# Views and If Else conditions

**URL:** <https://www.ruby-forum.com/t/views-and-if-else-conditions/124756>\
**Category:** Rails\
**Created:** [January 2, 2008, 9:16am UTC](https://www.ruby-forum.com/t/views-and-if-else-conditions/124756 "2008-01-02T09:16:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [January 2, 2008, 9:16am UTC](https://www.ruby-forum.com/t/views-and-if-else-conditions/124756/1 "2008-01-02T09:16:03Z")

</div>

Hello all,

How do I use nested if conditions in my views? I have something like

if(res.birthdate\_status == “0”)  
{  
if(res.gender == “M” || res.gender==“F”)  
print , //print a comma  
print res.age  
}

–  
Regards

Haris G.

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [January 2, 2008, 9:40am UTC](https://www.ruby-forum.com/t/views-and-if-else-conditions/124756/2 "2008-01-02T09:40:36Z")

</div>

Well, you _could_ do this, but I don’t like it:

\<% if @user.birthdate\_status == 0 %\>  
\<% if @user.gender == “M” || @user.gender == “F” %\>  
,  
\<% end %\>  
\<%=@user.age %\>  
\<% end %\>

However, Rails has helpers to make this kind of stuff a little more  
organized.

In your view, do this:

\<%=display\_age(@user) %\>

Then in helpers/application\_helper.rb, add this code (untested and not  
as  
pretty as it could be)

def display\_age(user)  
result = “”  
if user.birthday\_status == 0  
result = user.age  
if user.gender == “M” || user.gender == “F”  
result = “, #{result}” # the #{} syntax is for embedding  
expressions  
in strings  
end  
end  
return result  
end

Helpers keep your views clean. They’re a place to hide display logic.  
Business logic should go in your model, so don’t buse helpers 🙂

Does that help?

-Brian

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [January 2, 2008, 10:35am UTC](https://www.ruby-forum.com/t/views-and-if-else-conditions/124756/3 "2008-01-02T10:35:06Z")

</div>

Thanx brian, yeah it does help.

Please also tell me another thing. I have the following in my  
index.rhtml

\<% @result.each do |res| %\>  
\<%= res.first\_name %\>  
  
  
\<%= res.register.language\_speak %\>  
  
  
  
  
  
\<% end %\>

My models are as follows

class Profile \< ActiveRecord::Base  
has\_many :friends, :foreign\_key =\> “user\_id”  
has\_one :register, :foreign\_key =\> “id”

def self.table\_name() “mhp\_profile” end  
end

class Friend \< ActiveRecord::Base  
belongs\_to :profile

def self.table\_name() “mhp\_friends” end  
end

class Register \< ActiveRecord::Base  
belongs\_to :profile

def self.table\_name() “mhp\_register” end  
end

But Im getting the following error.

Showing _profile/index.rhtml_ where line _#4_ raised:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.language\_speak

Any idea what am I doing wrong…I have only one controller for profile  
as  
follows

class ProfileController \< ApplicationController  
def index  
@result = Profile.find(:all)  
end  
end

On Jan 2, 2008 1:39 PM, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, Rails has helpers to make this kind of stuff a little more  
> result = “”  
> Helpers keep your views clean. They’re a place to hide display logic.
> 
> > Hello all,
> > 
> > –  
> > Regards
> > 
> > Haris G.
> 
> >

–  
Regards

Haris G.

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [January 2, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/views-and-if-else-conditions/124756/4 "2008-01-02T14:08:27Z")

</div>

On Jan 2, 2008, at 3:34 AM, Haris G. wrote:

> Thanx brian, yeah it does help.
> 
> Please also tell me another thing. I have the following in my  
> index.rhtml
> 
> \<% @result.each do |res| %\>  
> \<%= res.first\_name %\>  
>   
>   
> \<%= res.register.language\_speak %\>

It sounds like you don’t have an associated “register” for this  
“res”. A very simple solution is to test register, such as

\<%= res.register.language\_speak if res.register %\>

or

\<%= res.register.language\_speak unless res.register.nil? %\>

depending on how your mind likes to conceptualize things.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![Haris\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/8edcca/32.png) [@Haris\_G](https://www.ruby-forum.com/u/Haris_G)\
**Post date:** [January 2, 2008, 4:33pm UTC](https://www.ruby-forum.com/t/views-and-if-else-conditions/124756/5 "2008-01-02T16:33:26Z")

</div>

On Jan 2, 2008 8:07 AM, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > \<%= res.first\_name %\>  
> > \<%= res.register.language\_speak unless res.register.nil? %\>
> 
> depending on how your mind likes to conceptualize things.

I’d be tempted to add a method on the profile model to return  
language\_speak (should this really be something like language\_spoken  
?).

class Profile  
…  
def language\_speak  
register.language\_speak if register  
end  
end

and then change the view code to just:

\<%= res.language\_speak %\>

The original code violates the so-called “law” of Demeter which says  
you shouldn’t reach through an object to get to another one, the  
canonical example of a demeter violation would be having a PaperBoy  
object which grabs the Wallet object from one if its Customers to  
extract payment for a subscription.

Besides the obvious rudeness, doing this with a method in the model  
you have directly at hand isolates the view code from potential future  
refactorings.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
