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 + ''''%> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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/>
On 1/8/06, Jeremy Evans <jeremyevans0@gmail.com> wrote:> 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/> > _______________________________________________ > 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
Jens-Christian Fischer
2006-Jan-09 10:08 UTC
[Rails] Nil column results, possible to ignore?
> 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 -- InVisible GmbH, Langgr?tstrasse 172, 8047 Z?rich Phone: +41 44 401 09 30 http://www.invisible.ch http://not.invisible.ch -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2361 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060109/60661c80/smime.bin
> 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
Jens-Christian Fischer
2006-Jan-09 12:54 UTC
[Rails] Nil column results, possible to ignore?
>> 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... :-)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... -- InVisible GmbH, Langgr?tstrasse 172, 8047 Z?rich Phone: +41 44 401 09 30 http://www.invisible.ch http://not.invisible.ch -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2361 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060109/47429c22/smime.bin
Enrico Teotti wrote:>>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... :-) >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