Okay so heres my code which works fine, i''ll explain the problem below. <% for num in 1..-04Gazou6+pmm/bHYDwDEOA@public.gmane.org %> <table class="products" id="table1" cellspacing="2" cellpadding="3" border="3"> <tr> <td> <table width="25%"> <tr> <tr> <td><%= @results[1].name%> | <%number_to_currency(@results[1].price)%></td> </tr> <tr> <td><%= link_to image_tag(@results[1].image_url, :size => ''100x100'', :border => "0")%></td> </tr> <tr> <td><%= h(truncate(@results[1].description, 150))%></td> </tr> </tr> </table> <td> </table> <%end%> This works fine, however when i replace the 1''s with num, which should work. i get the error. "You have a nil object when you didn''t expect it! The error occurred while evaluating nil.name." I know that num is outputting a number because if i throw <%=num%> somewhere in the loop it will output all the numbers. I''m stuck on this i''ve tried everything i can think of, I know there are other ways to loop through this but i need this way to work. Any help would be appreciated. Thanks. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Apr-24 15:04 UTC
Re: Please help. The error occurred while evaluating nil.name
On 24 Apr 2008, at 15:51, nameth wrote:> > Okay so heres my code which works fine, i''ll explain the problem > below. > > <% for num in 1..-04Gazou6+pmm/bHYDwDEOA@public.gmane.org %>array indices from from 0 to length -1, not from 1 to length Fred> > > "You have a nil object when you didn''t expect it! The error occurred > while evaluating nil.name." > > I know that num is outputting a number because if i throw <%=num%> > somewhere in the loop it will output all the numbers. I''m stuck on > this i''ve tried everything i can think of, I know there are other ways > to loop through this but i need this way to work. Any help would be > appreciated. Thanks. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nameth
2008-Apr-24 15:16 UTC
Re: Please help. The error occurred while evaluating nil.name
On Apr 24, 10:04 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 24 Apr 2008, at 15:51, nameth wrote: > > > > > Okay so heres my code which works fine, i''ll explain the problem > > below. > > > <% for num in 1...-04Gazou6+pmm/bHYDwDEOA@public.gmane.org %> > > array indices from from 0 to length -1, not from 1 to length > > Fredwill you marry me> > > > > "You have a nil object when you didn''t expect it! The error occurred > > while evaluating nil.name." > > > I know that num is outputting a number because if i throw <%=num%> > > somewhere in the loop it will output all the numbers. I''m stuck on > > this i''ve tried everything i can think of, I know there are other ways > > to loop through this but i need this way to work. Any help would be > > appreciated. Thanks.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
There''s a better way to use the @results collection, too. Some people prefer the vb/c# like iteration: for result in @results do Personally I don''t like that because it''s one more indirection than necessary. I prefer the fundamental each operator: @results.each do |result| In either case, you can use all the code you''ve got, but replace all the @results[num] references with result (or something more domain specific like product...). For example, <% @results.each do |product| %> ... <td><%= product.name%> | <%number_to_currency(product.price)%></td> This version expresses your intentions much more clearly, which will make it easier to maintain in the future.. On Apr 24, 11:16 am, nameth <Justin.McGin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Apr 24, 10:04 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 24 Apr 2008, at 15:51, nameth wrote: > > > > Okay so heres my code which works fine, i''ll explain the problem > > > below. > > > > <% for num in 1...-04Gazou6+pmm/bHYDwDEOA@public.gmane.org %> > > > array indices from from 0 to length -1, not from 1 to length > > > Fred > > will you marry me > > > > > > "You have a nil object when you didn''t expect it! The error occurred > > > while evaluating nil.name." > > > > I know that num is outputting a number because if i throw <%=num%> > > > somewhere in the loop it will output all the numbers. I''m stuck on > > > this i''ve tried everything i can think of, I know there are other ways > > > to loop through this but i need this way to work. Any help would be > > > appreciated. Thanks.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nameth
2008-Apr-24 16:54 UTC
Re: Please help. The error occurred while evaluating nil.name
Yeah i know i could just use <%for each prod in @results%> and then reference by <%=prod.name%> <%=prod.price%> etc... , but the thing is i don''t want to "do" what i''m doing for "each" thing in the @results collection. here is my finished page, you can probably see why i couldn''t do it that way. <div id="products"> <table cellspacing="2" cellpadding="3" border="3"> <% for prods in 0..@stop %> <tr> <%for i in 0..3%> <td> <table width="25%"> <tr> <td><%= @results[i].name%>| <%number_to_currency(@results[i].price)%></td> </tr> <tr> <td><%= link_to image_tag(@results[i].image_url, :size => ''100x100'', :border => "0")%></td> </tr> <tr> <td><%= h(truncate(@results[i].description, 150))%></td> </tr> </table> </td> <%end%> </tr> <%end%> </table> <% for whatsleft in 0..0%> <%newstop = (@results.length) - 1%> <table class="products" id="table1" cellspacing="2" cellpadding="3" border="3"> <tr> <%for i in 0..@whatsLeft - 1%> <td> <table width="25%"> <tr> <td><%= @results[newstop].name%>| <%number_to_currency(@results[newstop].price)%></td> </tr> <tr> <td><%= link_to image_tag(@results[newstop].image_url, :size => ''100x100'', :border => "0")%></td> </tr> <tr> <td><%= h(truncate(@results[newstop].description, 150))%></td> </tr> </table> <%newstop -= 1%> <%end%> </td> <%end%> </tr> </table> </div> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nameth wrote:>>> <% for num in 1...Next, never use ''for .. in'' in Ruby. Matz added it just to look like lessor languages; to occupy the third page of a Ruby tutorial. Use names.each do |name| instead. And BTW everyone knows that online romances never last... -- Phlip http://c2.com/cgi/wiki?AssertYinYang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Barney
2008-Apr-26 11:04 UTC
Re: Please help. The error occurred while evaluating nil.name
nameth, You don''t have to resort to old-school indexed iteration to solve this type of problem. Have a look at Rails'' ''grouping'' extentions to Array: http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Array/Grouping.html For example: <table> <% @results.in_groups_of(4).compact do |result_group| %> <tr> <% result_group.each do |product| %> <td> <%= h product.name %>| <%= number_to_currency product.price %> </td> <% end %> </tr> <tr> <% result_group.each do |product| %> <td> <%= link_to image_tag(product.image_url, :size => ''100x100'', :border => "0") %> </td> <% end %> </tr> <tr> <% result_group.each do |product| %> <td> <%= h(truncate(product.description, 150)) %> </td> <% end %> </tr> <% end %> </table> Should produce similar output, but with DRYer, cleaner code. - Barney On Apr 25, 2:54 am, nameth <Justin.McGin...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yeah i know i could just use <%for each prod in @results%> and then > reference by <%=prod.name%> <%=prod.price%> etc... , but the thing is > i don''t want to "do" what i''m doing for "each" thing in the @results > collection. here is my finished page, you can probably see why i > couldn''t do it that way. > > <div id="products"> > <table cellspacing="2" cellpadding="3" border="3"> > <% for prods in 0..@stop %> > <tr> > <%for i in 0..3%> > <td> > <table width="25%"> > <tr> > <td><%= @results[i].name%>| <%> number_to_currency(@results[i].price)%></td> > </tr> > <tr> > <td><%= link_to image_tag(@results[i].image_url, :size => > ''100x100'', :border => "0")%></td> > </tr> > <tr> > <td><%= h(truncate(@results[i].description, 150))%></td> > </tr> > </table> > </td> > <%end%> > </tr> > <%end%> > </table> > <% for whatsleft in 0..0%> > <%newstop = (@results.length) - 1%> > <table class="products" id="table1" cellspacing="2" cellpadding="3" > border="3"> > <tr> > <%for i in 0..@whatsLeft - 1%> > <td> > <table width="25%"> > <tr> > <td><%= @results[newstop].name%>| <%> number_to_currency(@results[newstop].price)%></td> > </tr> > <tr> > <td><%= link_to image_tag(@results[newstop].image_url, :size => > ''100x100'', :border => "0")%></td> > </tr> > <tr> > <td><%= h(truncate(@results[newstop].description, 150))%></td> > </tr> > </table> > <%newstop -= 1%> > <%end%> > </td> > <%end%> > </tr> > </table> > </div>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
nameth
2008-May-01 16:50 UTC
Re: Please help. The error occurred while evaluating nil.name
Thanks Barney, Exactly what i was looking for. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---