Forum: Ruby on Rails Easy way of dealing with nil properties in templates?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2006-02-07 03:32
(Received via mailing list)
Is there an easy way to deal with nil properties in
templates? All I'm aware of are these methods, and
it's quite tedious and surely violates DRY.

<%= @member.name unless @member.name.nil? %>
<%= @member.name.to_is %>
<%= "#{@member.name}" %>


csn

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Dwight Felkins (Guest)
on 2006-02-07 04:54
(Received via mailing list)
Hmmm. I don't have much of an improvement. I tend to use

?:

Instead of your methods I would put

<%= @member.name.nil? ? '' : h(@member.name) <http://member.name/> %>

And if I see it enough I will move it into a helper.

But I agree, it is tedious.

-Kelly
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2006-02-07 06:28
(Received via mailing list)
Yeah. You can also put begin/end around templates,
actions, probably handle a nil exception in
rescue_action_in_public, or even have database
defaults be '' rather than null, but I'm not keen on
any of those either.

csn

--- Kelly Dwight Felkins <railsinator@gmail.com>
wrote:

> And if I see it enough I will move it into a helper.
> in
> > __________________________________________________
> > _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-07 07:31
(Received via mailing list)
You can also do this although I don't think its any prettier ;)

<%= @member.name rescue nil %>

-Ezra


On Feb 6, 2006, at 7:53 PM, Kelly Dwight Felkins wrote:

> But I agree, it is tedious.
> <%= @member.name.to_is %>
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
73606b1d1042de19056f3d59492b652f?d=identicon&s=25 charlie bowman (Guest)
on 2006-02-07 16:53
This is the one thing I miss about perls Template-Toolkit.  If you
access an undefined value then nothings prints and you don't get an
error.  I wish there was a way in a configuration file to make erb act
this way.
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-02-07 17:45
(Received via mailing list)
CSN wrote:
>
I'm a bit puzzled by your question: <%= @member.name %> will produce the
Erb code _erbout.concat((@member.name).to_s) which, since nil.to_s ==
"", will result in "" being appended to _erbout.

Did you mean @member == nil?
Or defined?(@member)==false?
Or @member.has_attribute?("name") == false?

-- stefan

--
For rails performance tuning, see: http://railsexpress.de/blog
Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
6559f9b9607a89074ea5e238d31ffbc2?d=identicon&s=25 Kelly Dwight Felkins (Guest)
on 2006-02-07 18:00
(Received via mailing list)
Often you have some base record, with an association to another record
and
you need an attribute. For example, say you have a project task and you
want
to display the name of the person responsible -- you might have:

@task.user.login

If user is undefined then you are trying to do a .login on a nil object.
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-02-07 18:36
Kelly Dwight Felkins wrote:
> Often you have some base record, with an association to another record
> and
> you need an attribute. For example, say you have a project task and you
> want
> to display the name of the person responsible -- you might have:
>
> @task.user.login
>
> If user is undefined then you are trying to do a .login on a nil object.

You could override method_missing for NilClass to simply return nil when
an unknown method is called on nil.

Then something like..

<%= @member.name %>

will do nothing if @member==nil

require 'pp'
class NilClass
	def method_missing(*params)
		return nil
	end
end

pp @member.name   #=> nil if @member.name not defined

This also helps with nested hashes

a= {:test=>"test"}
pp a[:test]        #=> "test"
pp a[:test][:one]  #=> nil
pp a[:one]         #=> nil

This might have all sorts of other unintended side effects... I haven't
tested it completely.

_Kevin
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-02-07 18:41
(Received via mailing list)
Kelly Dwight Felkins wrote:
> Often you have some base record, with an association to another record and
> you need an attribute. For example, say you have a project task and you want
> to display the name of the person responsible -- you might have:
>
> @task.user.login
>
> If user is undefined then you are trying to do a .login on a nil object.
>
<%= task.user.login rescue nil %>

seems to do the job.

-- stefan

--
For rails performance tuning, see: http://railsexpress.de/blog
Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-02-07 18:41
(Received via mailing list)
Kevin Olbrich wrote:
>> If user is undefined then you are trying to do a .login on a nil object.
>>
>
> You could override method_missing for NilClass to simply return nil when
> an unknown method is called on nil.
>

don't do this. It screws up nil's semantics big time.

-- stefan

--
For rails performance tuning, see: http://railsexpress.de/blog
Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2006-02-07 18:50
(Received via mailing list)
--- Stefan Kaes <skaes@gmx.net> wrote:

> CSN wrote:
> > Is there an easy way to deal with nil properties
> in
> > templates? All I'm aware of are these methods, and
> > it's quite tedious and surely violates DRY.
> >
> > <%= @member.name unless @member.name.nil? %>
> > <%= @member.name.to_is %>
> > <%= "#{@member.name}" %>

> I'm a bit puzzled by your question: <%= @member.name
> %> will produce the
> Erb code _erbout.concat((@member.name).to_s) which,
> since nil.to_s ==
> "", will result in "" being appended to _erbout.

That doesn't seem to be the case for me. 'name' is
null in the members table, and this causes a nil
error:

<%= @member.name %>

csn


> Did you mean @member == nil?
> Or defined?(@member)==false?
> Or @member.has_attribute?("name") == false?
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2006-02-07 18:53
(Received via mailing list)
> From: charlie bowman <cbowmanschool@...>
> Subject: Re: Easy way of dealing with nil properties
in templates?
> Date: 2006-02-07 15:53:59 GMT (1 hour and 55 minutes
ago)
>
> This is the one thing I miss about perls
Template-Toolkit.  If you
> access an undefined value then nothings prints and
you don't get an
> error.  I wish there was a way in a configuration
file to make erb act
> this way.

Yeah, some Rails var like
'convert_nil_properties_to_strings' would be nice. But
then I'd also want nil METHODS and nil objects to
cause errors.

csn


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2006-02-07 19:00
(Received via mailing list)
> CSN wrote:
> > Is there an easy way to deal with nil properties
> in
> > templates? All I'm aware of are these methods, and
> > it's quite tedious and surely violates DRY.
> >
> > <%= @member.name unless @member.name.nil? %>
> > <%= @member.name.to_is %>
> > <%= "#{@member.name}" %>

Hmm, maybe a helper function would be better.
Something like (np = nil print):

<%=np @member.name %>

I'm not sure what the 'np' function would look like.
Anybody?

csn


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-07 19:44
(Received via mailing list)
Another way to handle this is to check for nil in your controller and
create
a proxy object to handle it. You don't have to save it so it shouldn't
change your model.

Bob Silva
http://www.railtie.net/
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-07 20:32
(Received via mailing list)
On Feb 7, 2006, at 9:59 AM, CSN wrote:

> Hmm, maybe a helper function would be better.
> Something like (np = nil print):
>
> <%=np @member.name %>
>
> I'm not sure what the 'np' function would look like.
> Anybody?
>
> csn

CSN-

	Here is a helper method that you can put in application_helper. It
will print the default value only if the block results in nil
otherwise it will print the statement inside the block.


def default(string=nil)
   begin
     yield
   rescue
     string
   end
end

	Then you call it like this:

<%= default("Title Unavailable") { @post.comment.title } %>

	If @post.comment.title throws a nil error, it will return Title
Unavailable. But if @post.comment.title is not nil it will print its
contens.

fe:

irb(main):018:0> def default(string=nil)
irb(main):019:1>   begin
irb(main):020:2*     yield
irb(main):021:2>   rescue
irb(main):022:2>     string
irb(main):023:2>   end
irb(main):024:1> end
=> nil
irb(main):025:0> bar = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):026:0> default("No Bars!") { bar.size }
=> 4
irb(main):027:0> default("No Bars!") { bar.im_with_stupid }
=> "No Bars!"

Cheers-

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
81194a50c0f9bd95d7832a77fdf371bd?d=identicon&s=25 CSN (Guest)
on 2006-02-08 01:01
(Received via mailing list)
Cool :). Small change:

def default(string='')
   begin
     yield
   rescue
     string
   end
end

That way, this should work too:

<%= default { @post.comment.title } %>


csn



--- Ezra Zygmuntowicz <ezra@yakima-herald.com> wrote:

> >>> <%= @member.name unless @member.name.nil? %>
> > Anybody?
> block.
> 	Then you call it like this:
> fe:
> => [1, 2, 3, 4]
> WebMaster
>
__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
32d1b70dcff9066a90976c0fb18acabe?d=identicon&s=25 Neil Dugan (Guest)
on 2006-02-08 01:32
(Received via mailing list)
CSN wrote:
>
>>
>>And if I see it enough I will move it into a helper.
>>
>>But I agree, it is tedious.
>>
>>-Kelly
>>

If something can come back nil. and it causes trouble I tend to use
<% $name = (@member.name) ? @member.name:"" %>

then use the '$name' instead.  But I have found that most times RoR
handles the 'nil' conditions just fine.
8546e5131cc66da30ccd062234a75b12?d=identicon&s=25 Frederick Ros (sleeper)
on 2006-02-08 06:44
(Received via mailing list)
Neil Dugan wrote :
| If something can come back nil. and it causes trouble I tend to use
| <% $name = (@member.name) ? @member.name:"" %>
|
| then use the '$name' instead.  But I have found that most times RoR
| handles the 'nil' conditions just fine.

It can be simplified into:

  <% $name = @member.name || "" %>


--
Frederick Ros aka Sleeper -- sleeper@jabber.fr

Use variable names that mean something.
            - The Elements of Programming Style (Kernighan & Plaugher)
This topic is locked and can not be replied to.