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 Gulzar
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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
On Jan 2, 2008 2:15 AM, Haris Gulzar
<harisgulzar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> 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 Gulzar
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 %>
<br>
<%= res.register.language_speak %>
<br><br><br><br>
<% 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 Hogan
<bphogan-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> 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
>
>
> On Jan 2, 2008 2:15 AM, Haris Gulzar
<harisgulzar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>
> > 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 Gulzar
> >
> >
> >
>
> >
>
--
Regards
Haris Gulzar
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Jan 2, 2008, at 3:34 AM, Haris Gulzar 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 %> > <br> > <%= 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 2, 2008 8:07 AM, Phillip Koebbe <phillipkoebbe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On Jan 2, 2008, at 3:34 AM, Haris Gulzar 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 %> > > <br> > > <%= 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.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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---