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
Kelly Dwight Felkins
2006-Feb-07 03:53 UTC
[Rails] Easy way of dealing with nil properties in templates?
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 On 2/6/06, CSN <cool_screen_name90001@yahoo.com> 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}" %> > > > csn > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060207/4837ee37/attachment.html
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:> 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 > > > On 2/6/06, CSN <cool_screen_name90001@yahoo.com> > 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}" %> > > > > > > csn > > > > __________________________________________________ > > Do You Yahoo!? > > Tired of spam? Yahoo! Mail has the best spam > protection around > > http://mail.yahoo.com > > _______________________________________________ > > 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 >__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Ezra Zygmuntowicz
2006-Feb-07 06:29 UTC
[Rails] Easy way of dealing with nil properties in templates?
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:> 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) %> > > And if I see it enough I will move it into a helper. > > But I agree, it is tedious. > > -Kelly > > > On 2/6/06, CSN < cool_screen_name90001@yahoo.com> 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}" %> > > > csn > > __________________________________________________ > Do You Yahoo!? > Tired of spam? Yahoo! Mail has the best spam protection around > http://mail.yahoo.com > _______________________________________________ > 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060207/898db364/attachment-0001.html
charlie bowman
2006-Feb-07 15:54 UTC
[Rails] Re: Easy way of dealing with nil properties in templates?
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. -- Posted via http://www.ruby-forum.com/.
Stefan Kaes
2006-Feb-07 16:45 UTC
[Rails] Easy way of dealing with nil properties in templates?
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}" %> > > > csn >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
Kelly Dwight Felkins
2006-Feb-07 16:58 UTC
[Rails] Easy way of dealing with nil properties in templates?
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. On 2/7/06, 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}" %> > > > > > > csn > > > 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060207/db4e271a/attachment.html
Kevin Olbrich
2006-Feb-07 17:36 UTC
[Rails] Re: Easy way of dealing with nil properties in templates?
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 -- Posted via http://www.ruby-forum.com/.
Stefan Kaes
2006-Feb-07 17:38 UTC
[Rails] Easy way of dealing with nil properties in templates?
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
Stefan Kaes
2006-Feb-07 17:40 UTC
[Rails] Re: Easy way of dealing with nil properties in templates?
Kevin Olbrich wrote:> 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. >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
--- 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
CSN
2006-Feb-07 17:53 UTC
[Rails] Re: Easy way of dealing with nil properties in templates?
> From: charlie bowman <cbowmanschool@...> > Subject: Re: Easy way of dealing with nil propertiesin templates?> Date: 2006-02-07 15:53:59 GMT (1 hour and 55 minutesago)> > This is the one thing I miss about perlsTemplate-Toolkit. If you> access an undefined value then nothings prints andyou don''t get an> error. I wish there was a way in a configurationfile 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
> 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
Bob Silva
2006-Feb-07 18:40 UTC
[Rails] Easy way of dealing with nil properties in templates?
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/ -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of CSN Sent: Tuesday, February 07, 2006 10:00 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Easy way of dealing with nil properties in templates?> 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 _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Zygmuntowicz
2006-Feb-07 19:29 UTC
[Rails] Easy way of dealing with nil properties in templates?
On Feb 7, 2006, at 9:59 AM, CSN 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}" %> > > 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? > > csnCSN- 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
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:> > On Feb 7, 2006, at 9:59 AM, CSN 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}" %> > > > > 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 > > > > _______________________________________________ > 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
Neil Dugan
2006-Feb-08 00:30 UTC
[Rails] Easy way of dealing with nil properties in templates?
CSN wrote:> 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: > > >>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 >>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.>> >>On 2/6/06, CSN <cool_screen_name90001@yahoo.com> >>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}" %> >>> >>> >>>csn >>> >>>__________________________________________________ >>>Do You Yahoo!? >>>Tired of spam? Yahoo! Mail has the best spam >> >>protection around >> >>>http://mail.yahoo.com >>>_______________________________________________ >>>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 >> >
Frederick Ros
2006-Feb-08 05:43 UTC
[Rails] Easy way of dealing with nil properties in templates?
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) -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060208/920f64ae/attachment.bin