Hi all, I''m in the middle of coding my first real Rails app (somewhat ironically having to do with actual trains), and I''m trying to be a good little Railer and adhere to the DRY principle. I''ve created a partial to display some line items. It''s called ''train," and it creates a table with a row for headings, and then it uses "for" to iterate over the line items and display them. It looks like this: <table> <tr> <th>...print column headings...</th> </tr> <% for t in train %> <tr> <td> ....print out a line item....</td> </tr> <% end %> </table> The problem is, it won''t work with only one line item present in the object that gets passed to the partial. I get this error: undefined method `each'' for #<Train:0xb769e3c8> When multiple line items are present, it works like a charm. Any suggestions on how to handle this? Thanks. Alison
Alison Rowland
2005-Sep-28 01:37 UTC
Re: Partials: Iterating over Single and Multiple Objects
Thanks for the suggestion. Could I put my object into an array and still get the Rails functionality of referencing things like @object.object.attribute? I guess I''m a little fuzzy on the typing going on in Rails. When I used debug() to look at my "train" variable with a single entry (e.g. from a particular record) vs. multiple entries (e.g. from a find(:all)), they both showed a type of "object." In the meantime, I came up with a different solution involving nested partials, which I like quite well, even though it''s a little bit more cumbersome. I now have partials ''train'' and ''train_lineitem,'' and ''train'' now looks like this: <% if train.respond_to?(:each) %> <% for t in train %> <%= render( :partial => ''train_lineitem'', :object => t ) %> <% end %> <% else %> <%= render( :partial => ''train_lineitem'', :object => train ) %> <% end %> More elegant solutions will still be gladly considered. :) --Alison On 9/27/05, Matt Jankowski <mjankowski-3V1LzDqIiU+4+7hldlPAjkEOCMrvLtNR@public.gmane.org> wrote:> Have your controller set up w/ an array called trains, and then do > something like: > > <% for train in trains %> > ... > <% end %> > > Better to just always have an array (with zero, one or many items) than > to have an object sometimes and an array others. > > > Alison Rowland wrote: > > Hi all, > > > > I''m in the middle of coding my first real Rails app (somewhat > > ironically having to do with actual trains), and I''m trying to be a > > good little Railer and adhere to the DRY principle. I''ve created a > > partial to display some line items. It''s called ''train," and it > > creates a table with a row for headings, and then it uses "for" to > > iterate over the line items and display them. It looks like this: > > > > <table> > > <tr> > > <th>...print column headings...</th> > > </tr> > > > > <% for t in train %> > > <tr> > > <td> ....print out a line item....</td> > > </tr> > > <% end %> > > </table> > > > > > > The problem is, it won''t work with only one line item present in the > > object that gets passed to the partial. I get this error: > > > > undefined method `each'' for #<Train:0xb769e3c8> > > > > When multiple line items are present, it works like a charm. > > > > Any suggestions on how to handle this? Thanks. > > > > Alison > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jarkko Laine
2005-Sep-28 06:20 UTC
Re: Partials: Iterating over Single and Multiple Objects
On 28.9.2005, at 4.37, Alison Rowland wrote:> Thanks for the suggestion. Could I put my object into an array and > still get the Rails functionality of referencing things like > @object.object.attribute?Yes. When you iterate over an array, its items are just the same objects that they''d otherwise be.> I guess I''m a little fuzzy on the typing > going on in Rails. When I used debug() to look at my "train" variable > with a single entry (e.g. from a particular record) vs. multiple > entries (e.g. from a find(:all)), they both showed a type of "object."find :all returns an array.> > In the meantime, I came up with a different solution involving nested > partials, which I like quite well, even though it''s a little bit more > cumbersome. I now have partials ''train'' and ''train_lineitem,'' and > ''train'' now looks like this: > > <% if train.respond_to?(:each) %> > <% for t in train %> > <%= render( :partial => ''train_lineitem'', > :object => t ) %> > <% end %> > <% else %> > <%= render( :partial => ''train_lineitem'', > :object => train ) %> > <% end %> > > More elegant solutions will still be gladly considered. :)Why do you ever want to pass a single train object to this table? I''d think that in cases where you *know* you''ll only have one object, you could use directly the train_lineitem partial. If you use find :all you always get an array no matter how many objects find returns. Then you can with good conscience always use the for..in method, or even cleaner, do it like this: <%= render :partial => "train_lineitem", :collection => trains %> No explicit iteration needed :-) //jarkko> > --Alison > > On 9/27/05, Matt Jankowski <mjankowski-3V1LzDqIiU+4+7hldlPAjkEOCMrvLtNR@public.gmane.org> wrote: > >> Have your controller set up w/ an array called trains, and then do >> something like: >> >> <% for train in trains %> >> ... >> <% end %> >> >> Better to just always have an array (with zero, one or many items) >> than >> to have an object sometimes and an array others. >> >> >> Alison Rowland wrote: >> >>> Hi all, >>> >>> I''m in the middle of coding my first real Rails app (somewhat >>> ironically having to do with actual trains), and I''m trying to be a >>> good little Railer and adhere to the DRY principle. I''ve created a >>> partial to display some line items. It''s called ''train," and it >>> creates a table with a row for headings, and then it uses "for" to >>> iterate over the line items and display them. It looks like this: >>> >>> <table> >>> <tr> >>> <th>...print column headings...</th> >>> </tr> >>> >>> <% for t in train %> >>> <tr> >>> <td> ....print out a line item....</td> >>> </tr> >>> <% end %> >>> </table> >>> >>> >>> The problem is, it won''t work with only one line item present in the >>> object that gets passed to the partial. I get this error: >>> >>> undefined method `each'' for #<Train:0xb769e3c8> >>> >>> When multiple line items are present, it works like a charm. >>> >>> Any suggestions on how to handle this? Thanks. >>> >>> Alison >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails