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.
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.
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?
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
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.
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.
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
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.
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
Neil D. 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.