Forum: Ruby on Rails Nil column results, possible to ignore?

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.
D9d1b31a337f46ac5e4dd56b55404f58?d=identicon&s=25 Dylan Markow (Guest)
on 2006-01-09 03:34
(Received via mailing list)
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-01-09 07:54
(Received via mailing list)
On 1/8/06, Dylan Markow <dylan@dylanmarkow.com> wrote:
> The following code throws a "You have a nil object" error when outputting my
> client list. Is there a way around this, short of writing a specific case
> for each possibility?
>
> <%= client.city + ', ' + client.state + ' ' + client.zip + '<br/>'%>

Try: <%= "#{client.city}, #{client.state} #{client.zip}<br/>"%>
Or: <%= client.city %>, <%= client.state %> <%= client.zip %><br/>
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2006-01-09 08:27
(Received via mailing list)
On 1/8/06, Jeremy Evans <jeremyevans0@gmail.com> wrote:
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

I'm going to briefly explain why you need to do it like this, and
hopefully you can avoid problems in the future.

When one of the fields is nil, you end up with ... nil + ... and of
course there is no + operator defined for a nil.  If you include them
directly in the string using #{client.state} format as shown above,
you never end up using an operator - which is simply invoking a method
on an object - on a nil.

Pat
5085ba37ba624a788e04a842cd09bd6e?d=identicon&s=25 Jens-Christian Fischer (Guest)
on 2006-01-09 11:10
(Received via mailing list)
> For a table "clients" there are city, state, and zip fields.
> However, for some clients we only have a state with no actual
> address. The following code throws a "You have a nil object" error
> when outputting my client list. Is there a way around this, short
> of writing a specific case for each possibility? <%= client.city +
> ', ' + client.state + ' ' + client.zip + '<br/>'%>

A neat thing to do in views is the following (which not quite fits
your case, but would help if you have the possibilty of the client
object being nil)

<%= client.city rescue "no city" -%>

I ususally use this for objects that are compounded via has_...
relationships:

<%= client.city.name rescue "no name defined" -%>

this is the short form of a

begin
    do some stuff
rescue
    there was an exception, let's deal with it
end

clause

hth jc
6992e8171ed4afa8b4ad4a8cef31fa29?d=identicon&s=25 Enrico Teotti (Guest)
on 2006-01-09 11:31
(Received via mailing list)
> A neat thing to do in views is the following (which not quite fits
> your case, but would help if you have the possibilty of the client
> object being nil)
>
> <%= client.city rescue "no city" -%>
>
> I ususally use this for objects that are compounded via has_...
> relationships:
It could be more MVC if that rescue thing was placed in the model,
wasn't it? Do you think is it possible?
Just food for though... :-)

--
"The only thing necessary for the triumph of evil
is for good men to do nothing"
 Edmund Burke
5085ba37ba624a788e04a842cd09bd6e?d=identicon&s=25 Jens-Christian Fischer (Guest)
on 2006-01-09 13:56
(Received via mailing list)
> Just food for though... :-)
I'm not sure that's the case (but willing to be convinced otherwise).
I usually see this code when I don't have complete control over the
data in the database but still need to be sure, that something
sensible is displayed. Usually, the controller gives me a bunch of
objects and some of them (or some of their relations) can be NIL. I'm
not sure how that could be handled by the model...
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-01-10 05:54
(Received via mailing list)
Enrico Teotti wrote:
> wasn't it? Do you think is it possible?
> Just food for though... :-)
>
I disagree... what to print if an object has no value is a display
issue... in fact, I
think it's a common use case to want to display different things on
different pages for
the nil case... or nothing as previous posters have pointed out (by
using #{}).

b
This topic is locked and can not be replied to.