following AWDWR book - ''Partials and Collections'' (rather sparse info there too) and I might add that the wiki page on this topic is entirely devoid of any info...be that as it may... I have hash of several arrays that I want to iterate though and display. the first hashed element looks like this (greatly simplified) - :facility: !ruby/object:Facility attributes: name: Toby I beds_a: "12" beds_m: "12" id: "1" beds_f: "12" :placements: [] My view code (fac_log_all_test) is a one liner... <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> According to the AWDWR book "Inside the partial, the local variable ''article'' will be set to the current article from the collection-the variable is named after the template. In addition, the variable article counter will be set to the index of the current article in the collection. According to my admittedly incomplete understanding, this should work, but it doesn''t... <%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %> ;-( what is the proper usage? Craig NoMethodError in # Showing app/views/reports/_fac_log_test.rhtml where line #28 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Extracted source (around line #28): 25: <tr> 26: <td class="form_small" width="25">FACILITY:</td> 27: <td class="form_small" width="5"></td> 28: <td class="form_small" width="110"><%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %></td>
Hello Craig, 2006/3/15, Craig White <craigwhite@azapple.com>:> My view code (fac_log_all_test) is a one liner... > <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> > > According to my admittedly incomplete understanding, this should work, but it doesn''t... > > <%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %>Drop the :fac_log_test_counter ! <%=h (fac_log_test[:facility][:name]) %> Running with :collection is equivalent to this: @facility_log_run.each do |fac_log_test| # partial view code here end Did you know about debug ? You could have done this to debug your code: <%=debug(fac_log_test) %> That would have shown you a YAML dump of the object. Hope the helps ! -- Fran?ois Beausoleil http://blog.teksol.info/
On Wed, 2006-03-15 at 23:19 -0500, Francois Beausoleil wrote:> Hello Craig, > > 2006/3/15, Craig White <craigwhite@azapple.com>: > > My view code (fac_log_all_test) is a one liner... > > <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> > > > > According to my admittedly incomplete understanding, this should work, but it doesn''t... > > > > <%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %> > > Drop the :fac_log_test_counter ! > > <%=h (fac_log_test[:facility][:name]) %>---- been there and done that... NoMethodError in # Showing app/views/reports/_fac_log_test.rhtml where line #28 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Extracted source (around line #28): 25: <tr> 26: <td class="form_small" width="25">FACILITY:</td> 27: <td class="form_small" width="5"></td> 28: <td class="form_small" width="110"><%=h (fac_log_test[:facility][:name]) %></td> -----> > Running with :collection is equivalent to this: > > @facility_log_run.each do |fac_log_test| > # partial view code here > end > > Did you know about debug ? You could have done this to debug your code: > <%=debug(fac_log_test) %> > > That would have shown you a YAML dump of the object.---- been there and done that... that''s where I got the listing that I gave you in the last email... - :facility: !ruby/object:Facility attributes: name: Toby I beds_a: "12" beds_m: "12" id: "1" beds_f: "12" :placements: [] though I used, <%= debug @facility_log %>, I think that if you look again, you would know why I used that instead. Trust me, I wouldn''t have asked the list if I hadn''t already exhausted the meager documentation (less than a full page in AWDWR), a header but no text on wiki and hours of trial and error. ----> Hope the helps !---- It didn''t but thanks for the effort. Craig
On Wed, 2006-03-15 at 23:19 -0500, Francois Beausoleil wrote:> Hello Craig, > > 2006/3/15, Craig White <craigwhite@azapple.com>: > > My view code (fac_log_all_test) is a one liner... > > <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> > > > > According to my admittedly incomplete understanding, this should work, but it doesn''t... > > > > <%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %> > > Drop the :fac_log_test_counter ! > > <%=h (fac_log_test[:facility][:name]) %> > > Running with :collection is equivalent to this: > > @facility_log_run.each do |fac_log_test| > # partial view code here > end > > Did you know about debug ? You could have done this to debug your code: > <%=debug(fac_log_test) %> > > That would have shown you a YAML dump of the object. > > Hope the helps !---- I should also explain that the reason that I added the fac_log_test_counter was because the only way I have ever been able to get data from the hash array was to use... <%=h (@facility_log[1][:facility][:name]) %> which actually works but if I try to use... <%=h (@facility_log[fac_log_test][:facility][:name]) %> TypeError in # Showing app/views/reports/_fac_log_test.rhtml where line #29 raised: no implicit conversion from nil to integer Extracted source (around line #29): 26: <tr> 27: <td class="form_small" width="25">FACILITY:</td> 28: <td class="form_small" width="5"></td> 29: <td class="form_small" width="110"><%=h (@facility_log[fac_log_test][:facility][:name]) %></td> craps out... Is it me or does it seem as though the book is a bit long in the tooth? Craig
On Wed, 2006-03-15 at 21:46 -0700, Craig White wrote:> On Wed, 2006-03-15 at 23:19 -0500, Francois Beausoleil wrote: > > Hello Craig, > > > > 2006/3/15, Craig White <craigwhite@azapple.com>: > > > My view code (fac_log_all_test) is a one liner... > > > <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> > > > > > > According to my admittedly incomplete understanding, this should work, but it doesn''t... > > > > > > <%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %> > > > > Drop the :fac_log_test_counter ! > > > > <%=h (fac_log_test[:facility][:name]) %> > > > > Running with :collection is equivalent to this: > > > > @facility_log_run.each do |fac_log_test| > > # partial view code here > > end > > > > Did you know about debug ? You could have done this to debug your code: > > <%=debug(fac_log_test) %> > > > > That would have shown you a YAML dump of the object. > > > > Hope the helps ! > ---- > I should also explain that the reason that I added the > fac_log_test_counter was because the only way I have ever been able to > get data from the hash array was to use... > > <%=h (@facility_log[1][:facility][:name]) %> > which actually works > > but if I try to use... > <%=h (@facility_log[fac_log_test][:facility][:name]) %> > TypeError in # > Showing app/views/reports/_fac_log_test.rhtml where line #29 raised: > > no implicit conversion from nil to integer > > Extracted source (around line #29): > > 26: <tr> > 27: <td class="form_small" width="25">FACILITY:</td> > 28: <td class="form_small" width="5"></td> > 29: <td class="form_small" width="110"><%=h (@facility_log[fac_log_test][:facility][:name]) %></td> > > craps out... > > Is it me or does it seem as though the book is a bit long in the tooth?---- nope - forgive me...it seems the combination of the instance variable @facility_log and the counter fac_log_test_counter does give me data...not at all what the book describes but it works. thanks Craig
On Wed, 2006-03-15 at 21:50 -0700, Craig White wrote: I know it''s bad form to reply to one''s self...but I think I have narrowed things down to one comprehensible posting. further information as I am very focused now on settling this issue. Things simply aren''t working like the AWDWR books suggests that it would. Since I already quoted from the book, I can simplify what is happening... My view code *** fac_log_test_all.rhtml *** <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> My controller code has... def fac_log_all_test @facility_log = facility_log_run( 1, 14 ) end def facility_log_run number, max results = [] number.upto(max) do |idx| facility = Facility.find( idx ) placements = Placement.find(:all, :conditions => [ "facility_id = ? and discharge_date IS NULL and admission_date IS NOT NULL", idx ]) results << { :facility => facility, :placements => placements } end results end and finally...the view code I am using in the partial ***_fac_log_test.rhtml*** <%=h (@facility_log[fac_log_test_counter][:facility][:name]) %> which actually prints - no errors except that only one page is rendered - (yes, I understand it has something to do with using the instance variable but I thought the point of using :collection in the :partial was that it would loop through each of the arrays.) The AWDWR book suggests that I can use a variable, named after the template (the partial) but it clearly doesn''t work... <%=h (fac_log_test[fac_log_test_counter][:facility][:name]) %> NoMethodError in # Showing app/views/reports/_fac_log_test.rhtml where line #28 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Extracted source (around line #28): 25: <tr> 26: <td class="form_small" width="25">FACILITY:</td> 27: <td class="form_small" width="5"></td> 28: <td class="form_small" width="110"><%=h (fac_log_test[fac_log_test_counter][:facility][:name]) %></td> so while the ''variable'' fac_log_test_counter seems to work like it is supposed to, the ''variable'' fac_log_test doesn''t - at least not at all like the AWDWR book works - and given this state that I am finding, it simply isn''t possible to iterate through the many records and display them all. There is something profoundly wrong but I can''t put my finger on it. Craig
Skipped content of type multipart/alternative-------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 2156 bytes Desc: not available Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/b7b3c9d2/smime.bin
On Wed, 2006-03-15 at 23:44 -0600, Dave Thomas wrote:> > On Mar 15, 2006, at 23:33, Craig White wrote: > > > There is something profoundly wrong but I can''t put my finger on it. > > > > Where is @facility_log_run getting set?---- it was in the code I posted... def fac_log_all_test @facility_log = facility_log_run( 1, 14 ) end I do as best as I can to trim things down to only the relevant items but enough to see the picture Craig
On Wed, 2006-03-15 at 23:44 -0600, Dave Thomas wrote:> > On Mar 15, 2006, at 23:33, Craig White wrote: > > > There is something profoundly wrong but I can''t put my finger on it. > > > > Where is @facility_log_run getting set?---- I''m sorry, I missed the word run... from the initial view code itself that calls the partials... My view code *** fac_log_test_all.rhtml *** <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> Craig
Skipped content of type multipart/mixed-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 250 bytes Desc: OpenPGP digital signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060316/54329671/signature.bin
Hi -- On Wed, 15 Mar 2006, Craig White wrote:> following AWDWR book - ''Partials and Collections'' (rather sparse info > there too) and I might add that the wiki page on this topic is entirely > devoid of any info...be that as it may... > > I have hash of several arrays that I want to iterate though and display. > > the first hashed element looks like this (greatly simplified) > > - :facility: !ruby/object:Facility > attributes: > name: Toby I > beds_a: "12" > beds_m: "12" > id: "1" > beds_f: "12" > :placements: [] > > My view code (fac_log_all_test) is a one liner... > <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> > > According to the AWDWR book "Inside the partial, the local variable ''article'' will be set to the current article from the collection-the variable is named after the template. In addition, the variable article counter will be set to the index of the current article in the collection. > > According to my admittedly incomplete understanding, this should work, but it doesn''t... > > <%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %>:fac_log_test_counter isn''t a variable; it''s a symbol. Try it without the : and see if that works. David -- David A. Black (dblack@wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" chapters now available from Manning Early Access Program! http://www.manning.com/books/black
On Thu, 2006-03-16 at 09:09 +0100, Mats Lindblad wrote:> Craig White wrote: > > On Wed, 2006-03-15 at 23:44 -0600, Dave Thomas wrote: > >> On Mar 15, 2006, at 23:33, Craig White wrote: > >> > >>> There is something profoundly wrong but I can''t put my finger on it. > >>> > >> Where is @facility_log_run getting set? > > ---- > > I''m sorry, I missed the word run... > > > > from the initial view code itself that calls the partials... > > > > My view code *** fac_log_test_all.rhtml *** > > <%= render :partial => ''fac_log_test'', :collection => @facility_log_run > > %> > > > > Craig > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > I think Daves point was that you are setting the collection > @facility_log and not @facility_log_run in the controller. > > Does that help?---- yes, that helps a lot - I''m sorry I didn''t understand the clue being given to me - I think that comes from too many hours on the same problem. That did permit it to iterate the ''outer'' loop and definitely solves my initial problem...I have to now figure out how to iterate over the inner loop but I will work it through myself before I ask the list for help. Interestingly enough though, using the ''collection'' as set in the controller still doesn''t fix the issue that I had to use the ''collection'' as the variable in my view code and it was not as described in the AWDWR book (Sept 2005 printing) - "The :collection parameter to render() can be used in conjunction with the :partial parameter. The :partial parameter lets us use a partial to define the format of an individual entry, and the :collection parameter applies this template to each member of the collection." and then the following paragraph it states that "the variable is named after the template" This had me chasing my tail for a long time because I kept trying to use the name of the partial as the variable name and I could find no acceptable methodology of making that work. In addition to this documentation issue, there were a number of people suggesting that I didn''t need to use the ''counter'' to identify the particular array element... @facility_log[fac_log_test_counter][:facility][:name] --------| array element ident| but I couldn''t find any way to make it work without that when iterating through the array. I can''t figure out if people just generally don''t do much iterating over arrays in views. Perhaps there were syntax changes since the AWDWR book was published and the fact that the wiki page was created on wiki.rubyonrails.org and then all data was erased makes it extremely hard to find the proper methodology. Craig
On Thu, 2006-03-16 at 04:52 -0800, dblack@wobblini.net wrote:> Hi -- > > On Wed, 15 Mar 2006, Craig White wrote: > > > following AWDWR book - ''Partials and Collections'' (rather sparse info > > there too) and I might add that the wiki page on this topic is entirely > > devoid of any info...be that as it may... > > > > I have hash of several arrays that I want to iterate though and display. > > > > the first hashed element looks like this (greatly simplified) > > > > - :facility: !ruby/object:Facility > > attributes: > > name: Toby I > > beds_a: "12" > > beds_m: "12" > > id: "1" > > beds_f: "12" > > :placements: [] > > > > My view code (fac_log_all_test) is a one liner... > > <%= render :partial => ''fac_log_test'', :collection => @facility_log_run %> > > > > According to the AWDWR book "Inside the partial, the local variable ''article'' will be set to the current article from the collection-the variable is named after the template. In addition, the variable article counter will be set to the index of the current article in the collection. > > > > According to my admittedly incomplete understanding, this should work, but it doesn''t... > > > > <%=h (fac_log_test[:fac_log_test_counter][:facility][:name]) %> > > :fac_log_test_counter isn''t a variable; it''s a symbol. Try it without > the : and see if that works.---- indeed - that was part of the fix...the other part as Dave suggested was the :collection => @facility_log (the array of data provided by the controller and not the method in the controller) thanks Craig
If this is flogging a dead horse, pass it by. My problem is solved and I have working code. Yesterday, probably owing to much frustration and my inability to adapt the information contained in an extremely brief section of the AWDWR book on ''partials and collections'' into my working code, I made the statement, "This had me chasing my tail for a long time because I kept trying to use the name of the partial as the variable name and I could find no acceptable methodology of making that work." Dave contacted me privately and suggested that "Folks use render(:partial => .., :collection => ..) in almost every rails application written. I promise you is works." and Dave is undoubtedly a lot smarter than I am and certainly light years ahead of my understanding of everything ruby/rails/programming and probably a number of other things as well. His book states that if I were to use... <%= render(:partial => ''fac_log_test'', :collection => @facility_log %> that this --------------^^^^^^^^^^^^ would be my variable in the view code and that I could use ''fac_log_test_counter'' as an automatic ''counter'' to use while iterating through the collection. Thus if my controller code specifically is def fac_log_all_test @facility_log = facility_log_run( 1, 14 ) end def facility_log_run number, max results = [] number.upto(max) do |idx| facility = Facility.find( idx ) placements = Placement.find(:all, :conditions => [ "facility_id = ?", idx ]) results << { :facility => facility, :placements => placements } end results end and the ''partial'' code includes ''my variable'' fac_log_test[fac_log_test_counter][:facility][:name] that each successive iteration would give me the next facility hashed inside the array. thus an array of hashes produced by above method from controller would look like the data below produced with <% debug @facility_log %> [1] The issue comes down to this... *** My understanding *** of the methodology suggested by Dave in AWDWR as listed above... within the partial, this should work... <%= fac_log_test[fac_log_test_counter][:facility][:name] %> which generates an error [2] what does work is... <%=h (@facility_log[fac_log_test_counter][:facility][:name]) %> which is not how the book describes it at all, ** as I understand it **. So either my usage is queer, my understanding was wrong or the methodology suggested in the book isn''t always applicable. Since there is a complete lack of information about how to iterate using :partials and :collections on wiki.rubyonrails.org, I am perfectly willing to provide a healthy write up of the process once I am sure that I understand it myself. So if anyone has any input into this process or can provide clarification...including Dave, please feel free to enlighten me and I promise to pass that knowledge on to the community via the wiki. Craig [1] - :facility: !ruby/object:Facility attributes: name: Toby I id: "1" :placements: - !ruby/object:Placement attributes: client_id: "32" admission_date: "2006-01-18" - :facility: !ruby/object:Facility attributes: name: Toby II :placements: - !ruby/object:Placement attributes: client_id: "33" admission_date: "2006-01-12" - !ruby/object:Placement attributes: client_id: "46" admission_date: "2006-03-12" [2] NoMethodError in # Showing app/views/reports/_fac_log_test.rhtml where line #28 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Extracted source (around line #28): 25: <tr> 26: <td class="form_small" width="25">FACILITY:</td> 27: <td class="form_small" width="5"></td> 28: <td class="form_small" width="110"><%= fac_log_test[fac_log_test_counter][:facility][:name] %></td>
I don''t think so. If you''re having this problem, then others probably are as well. My understanding of the counter was that it was orthogonal to the instance variable accessible to the partial. It''s mostly sugar so you can do even/odd striping. Here''s how I understand it to play out: # controller @foos = Foo.find(:all) => array of Foo''s # view render :partial => ''bar'', :collection => @foos # partial _bar <p class="<%= bar_counter.odd? ? ''red'' : ''white'' %>">this is iteration number <%= bar_counter %> and the individual array element passed in is <%= bar.some_random_attribute %>.</p> Google "Four Days on Rails" and dig through the PDF until you find the example of a striped table. I think it will bear out pretty much what I''ve written here. Craig White wrote:> If this is flogging a dead horse, pass it by. My problem is solved and I > have working code. >-- Posted via http://www.ruby-forum.com/.
On Fri, 2006-03-17 at 06:48 +0100, Steve Ross wrote:> I don''t think so. If you''re having this problem, then others probably > are as well. My understanding of the counter was that it was orthogonal > to the instance variable accessible to the partial. It''s mostly sugar so > you can do even/odd striping. > > Here''s how I understand it to play out: > > # controller > @foos = Foo.find(:all) => array of Foo''s > > # view > render :partial => ''bar'', :collection => @foos > > # partial _bar > <p class="<%= bar_counter.odd? ? ''red'' : ''white'' %>">this is iteration > number <%= bar_counter %> and the individual array element passed > in is <%= bar.some_random_attribute %>.</p> > > Google "Four Days on Rails" and dig through the PDF until you find the > example of a striped table. I think it will bear out pretty much what > I''ve written here. > > > Craig White wrote: > > If this is flogging a dead horse, pass it by. My problem is solved and I > > have working code. > >---- I had no issue with that at all...my ''counter'' worked...and I had ''striped'' tables working the first day I started my own rails coding - compliments of some very clear methodology in AWDWR Perhaps I didn''t make myself clear and if that''s the case, I probably wasted everyone''s time with the post and I won''t bother with fixing up the wiki because if I can''t make it clear to knowledgeable rails users, how am I ever gonna explain it to someone like me that is struggling with complicated methodology. interestingly enough, you are giving me the same explanation that Dave gave me...if it works for others...which is acceptable as long as you don''t have to worry about the details. Of course, with programming...it''s always the details. Craig
Skipped content of type multipart/mixed-------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 250 bytes Desc: OpenPGP digital signature Url : http://wrath.rubyonrails.org/pipermail/rails/attachments/20060317/003d9f84/signature.bin
I''ve been staying out of this thread, because it looked like plenty of other people had answered your questions, and you had Dave Thomas personally helping you. I didn''t see that I had much to add. But you''re still confused as to the basic nature of how partials with :collection work... Craig White wrote:> His book states that if I were to use... > > <%= render(:partial => ''fac_log_test'', :collection => @facility_log %> > that this --------------^^^^^^^^^^^^ > would be my variable in the view code and that I could use > ''fac_log_test_counter'' as an automatic ''counter'' to use while iterating > through the collection. > > Thus if my controller code specifically is > > def fac_log_all_test > @facility_log = facility_log_run( 1, 14 ) > end > > def facility_log_run number, max > results = [] > number.upto(max) do |idx| > facility = Facility.find( idx ) > placements = Placement.find(:all, > :conditions => [ "facility_id = ?", idx ]) > results << { :facility => facility, > :placements => placements } > end > results > endAll of the code above here is fine.> and the ''partial'' code includes ''my variable'' > > fac_log_test[fac_log_test_counter][:facility][:name] > > that each successive iteration would give me the next facility hashed > inside the array.Here''s where your problem has been, from the beginning. The fac_log_test variable, that is set each time the partial loops through the collection, is set to each successive item within the collection. It''s not a copy of the array of hashes, it''s set to each hash item within the array, each time the partial loops. That''s why the above keeps giving you an error. You need to change the statement to read: fac_log_test[:facility][:name]> thus an array of hashes produced by above method from controller would > look like the data below produced with <% debug @facility_log %> [1] > > The issue comes down to this... > > *** My understanding *** of the methodology suggested by Dave in AWDWR as listed above... > within the partial, this should work... > > <%= fac_log_test[fac_log_test_counter][:facility][:name] %> > > which generates an error [2]Unfortunately, as I stated above, your understanding of this aspect of partials with :collection is incorrect. The object you reference as :collection => needs to be an array. That array is then looped over, using the each_with_index method provided by the Enumerable mixin (for those who care ;). The partial only receives the array item, not the whole array.> what does work is... > <%=h (@facility_log[fac_log_test_counter][:facility][:name]) %> > > which is not how the book describes it at all, ** as I understand it **.Correct. Here you are simply referencing the instance variable that you generated so you could pass to the collection. If you were going to do this kind of referencing, you''re better off putting the loop in the main view template and skipping partials altogether.> So either my usage is queer, my understanding was wrong or the methodology > suggested in the book isn''t always applicable.Actually, what the book states is, in fact, correct. Unfortunately, somewhere along the way, you''re understanding of what the book was saying got muddled. And you''ve been operating under that mistaken understanding ever since.> Since there is a complete lack of information about how to iterate using > :partials and :collections on wiki.rubyonrails.org, I am perfectly willing to > provide a healthy write up of the process once I am sure that I understand it > myself.Actually, I never thought one was needed, but that''s because the AWDWR''s explanation was just fine. Did you ever look at the API page on partials? <http://api.rubyonrails.org/classes/ActionView/Partials.html>> So if anyone has any input into this process or can provide > clarification...including Dave, please feel free to enlighten me and I > promise to pass that knowledge on to the community via the wiki.Hopefully, I''ve been able to provide some clarification on where your understanding is off. If not, please ask. Now that I''m in the discussion, I''ll do my best to help you get over the hump on this. -Brian
On Fri, 2006-03-17 at 09:32 -0500, Brian V. Hughes wrote:> I''ve been staying out of this thread, because it looked like plenty of other > people had answered your questions, and you had Dave Thomas personally helping > you. I didn''t see that I had much to add. But you''re still confused as to the > basic nature of how partials with :collection work...---- but you have added much - see below ----> > Craig White wrote: > > His book states that if I were to use... > > > > <%= render(:partial => ''fac_log_test'', :collection => @facility_log %> > > that this --------------^^^^^^^^^^^^ > > would be my variable in the view code and that I could use > > ''fac_log_test_counter'' as an automatic ''counter'' to use while iterating > > through the collection. > > > > Thus if my controller code specifically is > > > > def fac_log_all_test > > @facility_log = facility_log_run( 1, 14 ) > > end > > > > def facility_log_run number, max > > results = [] > > number.upto(max) do |idx| > > facility = Facility.find( idx ) > > placements = Placement.find(:all, > > :conditions => [ "facility_id = ?", idx ]) > > results << { :facility => facility, > > :placements => placements } > > end > > results > > end > > All of the code above here is fine. > > > and the ''partial'' code includes ''my variable'' > > > > fac_log_test[fac_log_test_counter][:facility][:name] > > > > that each successive iteration would give me the next facility hashed > > inside the array. > > Here''s where your problem has been, from the beginning. The fac_log_test > variable, that is set each time the partial loops through the collection, is set > to each successive item within the collection. It''s not a copy of the array of > hashes, it''s set to each hash item within the array, each time the partial loops. > > That''s why the above keeps giving you an error. You need to change the statement > to read: > > fac_log_test[:facility][:name]---- which totally makes sense to me - the moment I read this, I said...yeah, that''s why I could never get it to work. So I changed it and the same brick wall occurred (you can see that I changed the line per your suggestion within the error)... NoMethodError in # Showing app/views/reports/_fac_log_test.rhtml where line #28 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Extracted source (around line #28): 28: <td class="form_small" width="110"><%=h (fac_log_test[:facility][:name]) %></td> I have left in all of the thread on this above to provide continuity and to suggest that I haven''t changed anything else but line #28 ----> > > thus an array of hashes produced by above method from controller would > > look like the data below produced with <% debug @facility_log %> [1] > > > > The issue comes down to this... > > > > *** My understanding *** of the methodology suggested by Dave in AWDWR as listed above... > > within the partial, this should work... > > > > <%= fac_log_test[fac_log_test_counter][:facility][:name] %> > > > > which generates an error [2] > > Unfortunately, as I stated above, your understanding of this aspect of partials > with :collection is incorrect. The object you reference as :collection => needs > to be an array. That array is then looped over, using the each_with_index method > provided by the Enumerable mixin (for those who care ;). The partial only > receives the array item, not the whole array.---- your explanation makes sense and that is how I thought I understood it and my problem is that it hasn''t worked for me. So at the moment, my instance variable with the array of hashes is the only thing that is working and where the variable itself hasn''t worked - and this is the focus of this entire discussion. ----> > > what does work is... > > <%=h (@facility_log[fac_log_test_counter][:facility][:name]) %> > > > > which is not how the book describes it at all, ** as I understand it **. > > Correct. Here you are simply referencing the instance variable that you > generated so you could pass to the collection. If you were going to do this kind > of referencing, you''re better off putting the loop in the main view template and > skipping partials altogether.---- My problem with doing that was that I had another loop in the loop which complicated it so much that I couldn''t make that work and I could probably go back to that knowing what I know now...but that seems so clumsy and I thought I could serve the community better in the process at this point. ----> > > So either my usage is queer, my understanding was wrong or the methodology > > suggested in the book isn''t always applicable. > > Actually, what the book states is, in fact, correct. Unfortunately, somewhere > along the way, you''re understanding of what the book was saying got muddled. And > you''ve been operating under that mistaken understanding ever since.---- agreed ----> > > Since there is a complete lack of information about how to iterate using > > :partials and :collections on wiki.rubyonrails.org, I am perfectly willing to > > provide a healthy write up of the process once I am sure that I understand it > > myself. > > Actually, I never thought one was needed, but that''s because the AWDWR''s > explanation was just fine. Did you ever look at the API page on partials? > <http://api.rubyonrails.org/classes/ActionView/Partials.html>---- Actually, I hadn''t checked that out and I probably should have. The explanation is somewhat similar to AWDWR and like most things in the api (which I wonder if I have been singlehandedly wearing out), is terse and no examples ;-) The thing that struck me as I looked at the api page referenced, is the notation... NOTE: Due to backwards compatibility concerns, the collection can?t be one of hashes. Normally you?d also just keep domain objects, like Active Records, in there. I ***think*** I have an ''array'' of hashes which I think is different than what is cautioned but being the newbie that I am, I am never really quite certain which is sort of why I published the ''debug'' at the end of my last post. ----> > > So if anyone has any input into this process or can provide > > clarification...including Dave, please feel free to enlighten me and I > > promise to pass that knowledge on to the community via the wiki. > > Hopefully, I''ve been able to provide some clarification on where your > understanding is off. If not, please ask. Now that I''m in the discussion, I''ll > do my best to help you get over the hump on this.---- Your explanation was perfectly clear - provided amplification to the AWDWR book explanation - if it had worked, the ''aha'' moment would have been all encompassing and I would move on (but create the wiki). As for the need/desirability of a wiki page...I think it''s self evident. The problem is that neither you nor Dave nor a lot of people on this list can actually see rails through a new users eyes any longer and what may appear obvious or to be a clear explanation in AWDWR or the api pages is less clear to a newer user. Iterating loops within loops over an array of hashes (with hashes in the hashes) has been a daunting experience. So I started with my best assessment on how to do this and ultimately ended up trying every conceivable possibility just out of frustration. Sometimes a parallel explanation using different terminology to convey the same information clicks. Craig
Craig White wrote:> On Fri, 2006-03-17 at 09:32 -0500, Brian V. Hughes wrote: >> That''s why the above keeps giving you an error. You need to change the statement >> to read: >> >> fac_log_test[:facility][:name] > ---- > which totally makes sense to me - the moment I read this, I said...yeah, > that''s why I could never get it to work. So I changed it and the same > brick wall occurred (you can see that I changed the line per your > suggestion within the error)... > > NoMethodError in # > Showing app/views/reports/_fac_log_test.rhtml where line #28 raised: > > You have a nil object when you didn''t expect it! > You might have expected an instance of Array. > The error occured while evaluating nil.[] > > Extracted source (around line #28): > > 28: <td class="form_small" width="110"><%=h (fac_log_test[:facility][:name]) %></td> > > I have left in all of the thread on this above to provide continuity and to suggest that I haven''t changed anything else but line #28OK. What you need to do is figure out what''s actually appearing in the fac_log_test variable. Since it''s in a partial and the partial is throwing an error, you''ll have to do some web dev trickery. What I would do is add a line above line 28 (I''m sort of interested to know what else is in your partial, 28 lines is a big block of lines to be iterating over) that gives you a way to see the value. Try this: <% session[:fac_log_test] = fac_log_test %> Now, when the partial errors out on you, you''ll be able to view the current value of the session, which should contain the fac_log_test value. This should shed some light on why trying to reference it like a Hash is throwing an error.> your explanation makes sense and that is how I thought I understood it > and my problem is that it hasn''t worked for me. So at the moment, my > instance variable with the array of hashes is the only thing that is > working and where the variable itself hasn''t worked - and this is the > focus of this entire discussion.Yes. But the reason that''s working is because you aren''t changing that instance variable after you set it in your controller. I''m guessing that you are inadvertently changing the value/type of the fac_log_test variable somewhere in your partial, before you get to line 28.> My problem with doing that was that I had another loop in the loop which > complicated it so much that I couldn''t make that work and I could > probably go back to that knowing what I know now...but that seems so > clumsy and I thought I could serve the community better in the process > at this point.Hmm... this could be part of the problem with us trying to provide assistance. The way that you are describing what you''re doing indicates to me that there''s potentially a lot of context that we, on the list, aren''t seeing. We definitely appreciate you trying to distill the problem down for us, but I think something might have been lost in the translation.> The thing that struck me as I looked at the api page referenced, is the > notation... > NOTE: Due to backwards compatibility concerns, the collection can?t be > one of hashes. Normally you?d also just keep domain objects, like Active > Records, in there. > > I ***think*** I have an ''array'' of hashes which I think is different > than what is cautioned but being the newbie that I am, I am never really > quite certain which is sort of why I published the ''debug'' at the end of > my last post.And the debug does help. Unfortunately, what you showed us was a debug from the instance variable that you''re passing to :collection. We''ve already discovered that variable isn''t changing, which is why accessing it through the _counter variable is working.> Your explanation was perfectly clear - provided amplification to the > AWDWR book explanation - if it had worked, the ''aha'' moment would have > been all encompassing and I would move on (but create the wiki). > > As for the need/desirability of a wiki page...I think it''s self evident.Well, I never said that a Wiki page wouldn''t be helpful, only that I didn''t see a need for it. :)> The problem is that neither you nor Dave nor a lot of people on this > list can actually see rails through a new users eyes any longer and what > may appear obvious or to be a clear explanation in AWDWR or the api > pages is less clear to a newer user.That''s probably true, to some extent. However, I, like Mr. Thomas (and I''m sure, many others on the list), spend a good portion of our time helping new users become more accomplished users. And not just with Rails. So I''m not sure your characterization is entirely correct.> Iterating loops within loops over an array of hashes (with hashes in the > hashes) has been a daunting experience.Um... wow. That would be a daunting experience, even for a very experienced Rails developer. That level of nesting is enough to make anyone feel lost and frustrated. It''s also one of the hardest things to debug when one portion of the process goes awry.> So I started with my best assessment on how to do this and > ultimately ended up trying every conceivable possibility just out of > frustration. Sometimes a parallel explanation using different > terminology to convey the same information clicks.Agreed. And hopefully, we''re going to get to the bottom of why your partial w/collection isn''t working the way partials w/collections are supposed to work... -Brian
On Fri, 2006-03-17 at 11:09 -0500, Brian V. Hughes wrote:> > which totally makes sense to me - the moment I read this, I said...yeah, > > that''s why I could never get it to work. So I changed it and the same > > brick wall occurred (you can see that I changed the line per your > > suggestion within the error)... > > > > NoMethodError in # > > Showing app/views/reports/_fac_log_test.rhtml where line #28 raised: > > > > You have a nil object when you didn''t expect it! > > You might have expected an instance of Array. > > The error occured while evaluating nil.[] > > > > Extracted source (around line #28): > > > > 28: <td class="form_small" width="110"><%=h (fac_log_test[:facility][:name]) %></td> > > > > I have left in all of the thread on this above to provide continuity and to suggest that I haven''t changed anything else but line #28 > > OK. What you need to do is figure out what''s actually appearing in the > fac_log_test variable. Since it''s in a partial and the partial is throwing an > error, you''ll have to do some web dev trickery. What I would do is add a line > above line 28 (I''m sort of interested to know what else is in your partial, 28 > lines is a big block of lines to be iterating over) that gives you a way to see > the value. Try this: > > <% session[:fac_log_test] = fac_log_test %> > > Now, when the partial errors out on you, you''ll be able to view the current > value of the session, which should contain the fac_log_test value. This should > shed some light on why trying to reference it like a Hash is throwing an error.---- thanks for the education in debugging...I had never thought of doing that. I know you''re gonna slap your forehead and say...of course - that''s what the error message was telling us... the session dump shows fac_log_test to be nil (not blank "") as for why line 28...it''s because of all the html setup code <html><meta tags><link tags to javascripts><style tags><body><table><tbody><tr><td> stuff - each ''partial'' actually becomes a fully independent ''page'' as there is a div with a page break at the bottom before the closing </html> tag as I am iterating each ''facility'' into a separately printed document. ----> > > your explanation makes sense and that is how I thought I understood it > > and my problem is that it hasn''t worked for me. So at the moment, my > > instance variable with the array of hashes is the only thing that is > > working and where the variable itself hasn''t worked - and this is the > > focus of this entire discussion. > > Yes. But the reason that''s working is because you aren''t changing that instance > variable after you set it in your controller. I''m guessing that you are > inadvertently changing the value/type of the fac_log_test variable somewhere in > your partial, before you get to line 28.---- not a chance of that...the only two erb lines in the document are... <% session[:fac_log_test] = fac_log_test %> <%=h (fac_log_test[:facility][:name]) %> ---> > > My problem with doing that was that I had another loop in the loop which > > complicated it so much that I couldn''t make that work and I could > > probably go back to that knowing what I know now...but that seems so > > clumsy and I thought I could serve the community better in the process > > at this point. > > Hmm... this could be part of the problem with us trying to provide assistance. > The way that you are describing what you''re doing indicates to me that there''s > potentially a lot of context that we, on the list, aren''t seeing. We definitely > appreciate you trying to distill the problem down for us, but I think something > might have been lost in the translation.---- reasonable...I chopped the file down to a minimum (I think) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> <% session[:fac_log_test] = fac_log_test %> <%=h (fac_log_test[:facility][:name]) %> </body> </html> and when re-run... NoMethodError in # Showing app/views/reports/_fac_log_test.rhtml where line #8 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Extracted source (around line #8): 5: </head> 6: <body> 7: <% session[:fac_log_test] = fac_log_test %> 8: <%=h (fac_log_test[:facility][:name]) %> ----> > I ***think*** I have an ''array'' of hashes which I think is different > > than what is cautioned but being the newbie that I am, I am never really > > quite certain which is sort of why I published the ''debug'' at the end of > > my last post. > > And the debug does help. Unfortunately, what you showed us was a debug from the > instance variable that you''re passing to :collection. We''ve already discovered > that variable isn''t changing, which is why accessing it through the _counter > variable is working.---- I hope that functional in a broken way doesn''t become a personal identification for me ;-) ----> > Iterating loops within loops over an array of hashes (with hashes in the > > hashes) has been a daunting experience. > > Um... wow. That would be a daunting experience, even for a very experienced > Rails developer. That level of nesting is enough to make anyone feel lost and > frustrated. It''s also one of the hardest things to debug when one portion of the > process goes awry.---- the scale of my task compared with the less than a full page of explanation within AWDWR seemed to be unequal but like everything else, when you have to clear the forest, you start by chopping the first tree. I have enough trees down at this point that I see a small clearing. ----> > > So I started with my best assessment on how to do this and > > ultimately ended up trying every conceivable possibility just out of > > frustration. Sometimes a parallel explanation using different > > terminology to convey the same information clicks. > > Agreed. And hopefully, we''re going to get to the bottom of why your partial > w/collection isn''t working the way partials w/collections are supposed to work...---- thanks - recognize again (and I think you do) is that I am working now by using the instance variable and the variable counter which is indeed working and my efforts here were to obtain confirmation that things work per documentation, possibly simplification to my code (as you can imagine, it''s not very DRY when I have to have separate ''partials'' for what is essentially the same document whether I print for one specific facility or iterate over all facilities and lastly, if I can claim clarity of knowledge, pour forth that knowledge into something usable for the next person. Craig
Craig-- The reason you are getting the same responses from people is that they are all doing the same thing with partials from collections and it''s working. You haven''t said where you get your instance variable and I assumed (as I suspect many will) that you are working with an AR derived type. If that''s not the case, we may be barking up the wrong tree. If it is an AR derived type, you should only have an array of hashes, and if you have associations the relationed fields will be arrays of hashes. Please consider my recommendation of Four Days (http://www.rubyonrails.com/media/text/Rails4Days.pdf). On about page 22, it describes using partials to display data in a collection. It uses the old syntax of render_collection_of_partials, but you can map that to render :partial => ''some'', :collection => @thing -- View this message in context: http://www.nabble.com/iterating-a-partial-with-%3Acollection-t1289209.html#a3466020 Sent from the RubyOnRails Users forum at Nabble.com.
On Fri, 2006-03-17 at 15:21 -0800, s.ross wrote:> Craig-- > > The reason you are getting the same responses from people is that they are > all doing the same thing with partials from collections and it''s working. > You haven''t said where you get your instance variable and I assumed (as I > suspect many will) that you are working with an AR derived type. If that''s > not the case, we may be barking up the wrong tree.---- actually, I did provide the exact code from which I derive the instance variable...and carried it forward for a few replies until it got in the way and you replied to the exact message that had that code yesterday but I will provide it again since you are claiming that I haven''t said where the instance variable is coming from... def fac_log_all_test @facility_log = facility_log_run( 1, 14 ) end def facility_log_run number, max results = [] number.upto(max) do |idx| facility = Facility.find( idx ) placements = Placement.find(:all, :conditions => [ "facility_id = ?", idx ]) results << { :facility => facility, :placements => placements } end results end As you can see, @facility_log (my instance variable) is derived from the method facility_log_run and is an array of hashes (facility) with another hash inside the array of hashes (placements) and given my understanding, this should qualify that I am working with an AR derived type. I also provided an excessively trimmed down version of the resultant value of the instance variable at the moment of :render with :collection ----> > If it is an AR derived type, you should only have an array of hashes, and if > you have associations the relationed fields will be arrays of hashes.---- In a sense, yes, the ''Facility'' model has_many :placements and it is this relationship that comprises the internal loop or the placements has inside the facility hash within the array that is the @facility_log instance variable. ----> > Please consider my recommendation of Four Days > (http://www.rubyonrails.com/media/text/Rails4Days.pdf). On about page 22, it > describes using partials to display data in a collection. It uses the old > syntax of render_collection_of_partials, but you can map that to---- but once again, just like yesterday, you are telling me two things - and I don''t dispute the validity of either of them... 1 - people are using it. 2 - look at this other example. The issue that people are using it is something I don''t argue. But a function that is wrapped within another function and breaks because it is wrapped inside another function neither proves that the function doesn''t work nor that it does work...it suggests that the function might be fragile and possibly what is known as a bug. I am not smart enough about these things to know that it is a bug and don''t have the toolsets yet to figure this out. The issue of looking at this other example is valid to a point, but I''ve seen working examples and agree that in most instances it does work. The instances are simplifications of model, view and controller code. Does that mean that in more complicated multi-tiered arrays/hashes that it will always work? I am perfectly willing to put this to rest at this point - apparently it depends upon the willingness of Brian Hughes to stay involved - as I have stated, I have a methodology which is working and I can move forward but it is not as the documentation says it should be and that bothered me (and evidently bothered Dave because I gave sentiment to that feeling with specific reference to the AWDWR book). The larger issue that bothered me was the really scant amount of documentation to the usage of render :partial and :collection as it does represent a fairly sophisticated application and at the very least in my case, was problematic but admittedly, that may be because I am not too bright. I will quote a related thread on another list on this subject from someone who is listed in the credits in Dave''s ''PickAxe'' book... "I found partials amazingly obtuse. The docs *are* unclear. Three or four Rails apps later, I still keep figuring out stuff. I have a lot of "Oh, so *that''s* how that''s supposed to work" moments, I think because the code and API revolves around how one or a small number of people just happened to have evolved the code, while observing their own habits. So it sometimes seems that the design is not based on well-reasoned choices to improve clarity or simplicity, but a formalizing of the habits the developers acquired as they got used to their own code." I can appreciate that few if any want to work through the issue with me as it entirely possible if not probable that it will ultimately serve little purpose other than to find bad code from newbie programmer at a too large investment in time...but recognize that it may be valid for me to believe that underlying structure that works properly in simple MVC form can break when the application is less than simple MVC. Craig
Just taking a wild stab at this, it appears you are throwing does_something_with collections at a collection of collections (i.e., an array of arrays of AR objects). Yes, the design does presume some coupling with the intended use. Not looking at the Rails code, I can''t say for certain what those assumptions are. Here''s how it looks like it might work (if I understand your code): def fac_log_all_test @facility_log = facility_log_run( 1, 14 ) end def facility_log_run number, max results = [] number.upto(max) do |idx| facility = Facility.find( idx ) placements = Placement.find(:all, :conditions => [ "facility_id = ?", idx ]) results << { :facility => facility, :placements => placements } end results end # view <%= render :partial => ''log_pages'', :collection => @facility_log %> # _log_pages Page <%= log_pages_counter %><br /> <%= render :partial => ''log_page'', :collection => ''log_pages'' %> # _log_page # handles each line in the AR array returned from an individual find -- View this message in context: http://www.nabble.com/iterating-a-partial-with-%3Acollection-t1289209.html#a3466745 Sent from the RubyOnRails Users forum at Nabble.com.
On Fri, 2006-03-17 at 16:34 -0800, s.ross wrote:> Just taking a wild stab at this, it appears you are throwing > does_something_with collections at a collection of collections (i.e., an > array of arrays of AR objects). Yes, the design does presume some coupling > with the intended use. Not looking at the Rails code, I can''t say for > certain what those assumptions are. Here''s how it looks like it might work > (if I understand your code): > > def fac_log_all_test > @facility_log = facility_log_run( 1, 14 ) > end > > def facility_log_run number, max > results = [] > number.upto(max) do |idx| > facility = Facility.find( idx ) > placements = Placement.find(:all, > :conditions => [ "facility_id = ?", idx ]) > results << { :facility => facility, > :placements => placements } > end > results > end > > # view > <%= render :partial => ''log_pages'', :collection => @facility_log %> > > # _log_pages > Page <%= log_pages_counter %><br /> > <%= render :partial => ''log_page'', :collection => ''log_pages'' %> > > # _log_page > # handles each line in the AR array returned from an individual find---- I have company coming for dinner now so I will check this out and play with the concept later tonight. Thanks Craig
On Fri, 2006-03-17 at 16:34 -0800, s.ross wrote:> Just taking a wild stab at this, it appears you are throwing > does_something_with collections at a collection of collections (i.e., an > array of arrays of AR objects). Yes, the design does presume some coupling > with the intended use. Not looking at the Rails code, I can''t say for > certain what those assumptions are. Here''s how it looks like it might work > (if I understand your code): > > def fac_log_all_test > @facility_log = facility_log_run( 1, 14 ) > end > > def facility_log_run number, max > results = [] > number.upto(max) do |idx| > facility = Facility.find( idx ) > placements = Placement.find(:all, > :conditions => [ "facility_id = ?", idx ]) > results << { :facility => facility, > :placements => placements } > end > results > end > > # view > <%= render :partial => ''log_pages'', :collection => @facility_log %> > > # _log_pages > Page <%= log_pages_counter %><br /> > <%= render :partial => ''log_page'', :collection => ''log_pages'' %> > > # _log_page > # handles each line in the AR array returned from an individual find---- that didn''t work at all... NoMethodError in # Showing app/views/reports/_log_page.rhtml where line #8 raised: You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[] Extracted source (around line #8): 5: </head> 6: <body> 7: <%# session[:facility_log] = log_pages %> 8: <%=h (log_page[:facility][:name]) %> just to confirm...this is what I have... reports_controller.rb (unchanged all along) both methods...unchanged def fac_log_all def fac_log_run fac_log_all.rthml (one line) <%= render :partial => ''log_pages'', :collection => @facility_log %> _log_pages.rhtml Page <%= log_pages_counter %><br /> <%= session[:facility_log] = log_pages %> <%= render :partial => ''log_page'', :collection => ''log_pages'' %> _log_page.rhtml <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> </head> <body> <%# session[:facility_log] = log_pages %> <%=h (log_page[:facility][:name]) %> </body> </html> Same problem...just more files. The interesting thing to note...is that if you look above, I stored the log_pages ''variable'' to the session in the partial _log_pages.rhtml When I inspected the session variables from the NoMethod error, it too was nil. Thus if log_pages variable is nil, it can''t possibly pass anything of value to log_page in the method that you are suggesting. In fact, it makes sense by extension...if the calling variable is nil, no matter how many times you call the partial, the result will still be nil. So the attack methodology was trying to close the barn after the horse was already gone. Craig
I think maybe the best way to proceed is to make certain the instance variable is of the type you expect. I suggest: <% logger.debug "hi! were in _log_page iteration #{log_page_counter}" %> <% logger.debug "hi, again! log_page is #{log_page||''nonexistent''} of type #{log_page.class}" %> I''m just typing this out of my head, so there may be some syntax glitches, but if you place one of these appropriate to each of the partials as the first line executed, the log might provide an interesting insight. In particular, it might help us explain why you are getting a nil value. Another thing I noticed is that you are using symbols as og_page[:facility][:name]. Try og_page[:facility][''name''] I hope some of this leads down the right path. -- View this message in context: http://www.nabble.com/iterating-a-partial-with-%3Acollection-t1289209.html#a3468194 Sent from the RubyOnRails Users forum at Nabble.com.
Hi, I tried to render partial with a collection = [{}, {}, {}]. The local variable in the partial is nil, which Craig found already and the counter variable is stuck at 1. I think the backward compatibility note that Craig found in the API doc is exactly what we are trying to do here so it won''t work with render partial. I was able to work around it by iterating it myself and use the 2nd partial that Steve suggested. Cheers, Andy
On Fri, 2006-03-17 at 21:06 -0800, s.ross wrote:> I think maybe the best way to proceed is to make certain the instance > variable is of the type you expect. I suggest: > > <% logger.debug "hi! were in _log_page iteration #{log_page_counter}" %> > <% logger.debug "hi, again! log_page is #{log_page||''nonexistent''} of type > #{log_page.class}" %> > > I''m just typing this out of my head, so there may be some syntax glitches, > but if you place one of these appropriate to each of the partials as the > first line executed, the log might provide an interesting insight. In > particular, it might help us explain why you are getting a nil value.---- OK - it makes sense and in fact, I became highly motivated since something broke and even though I saved my ''working code'' separately, it now no longer increments the @facility_log via the counter - getting stuck as Andy Shen commented and this is something from which I have to remove the chance element...it must have worked once and ''cached'' results ;-( Anyone who has tracked this thread would recognize that I am not a happy camper here. Anyway, I think I see where you are headed with this and with some fixes...I got this far, Rendering reports/fac_log_all_test hi! were in _log_pages iteration 1 hi, again! log_pages is nonexistent of type NilClass hi! were in _log_page iteration 0 hi, again! log_page is log_pages of type String ActionView::TemplateError (You have a nil object when you didn''t expect it! You might have expected an instance of Array. The error occured while evaluating nil.[]) on line #11 of app/views/reports/_log_page.rhtml: 8: </head> 9: <body> 10: <%# session[:facility_log] = log_pages %> 11: <%=h (log_page[:facility][:name]) %> which suggests to me that the ''counter'' of fac_log_all_test is stuck before the data gets through to _log_pages and I can''t explain why it doesn''t show iteration 0 for _log_pages but starts at iteration 1 ----> > Another thing I noticed is that you are using symbols as > og_page[:facility][:name]. Try og_page[:facility][''name''] > > I hope some of this leads down the right path.---- I think I''m dead before it get''s this far, but changing the symbol [:name] to [''name] on line 11 per above made no difference. Thanks Craig
Craig, Two points: 1. Learn how to use a feature on a trivial example before trying to weave its use into your apparently complex application. I suggest you keep a separate ''sandbox'' Rails application for trying things out. Had you done this, you should have arrived at the correct understanding of the AWDR description of partials before getting bogged down in their use in your application - or, failing that, you would have had a small focused example to discuss on the list. (Also, if you are not already in the habit, use irb to try out Ruby features and use script/console to test fragments of intended controller functionality against your models.) 2. How on earth can you have all this HTML boilerplate in a partial?> _log_page.rhtml > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > > <html xmlns="http://www.w3.org/1999/xhtml"> > <head> > </head> > <body> > <%# session[:facility_log] = log_pages %> > <%=h (log_page[:facility][:name]) %> > </body> > </html>You can put all the non-content stuff in a layout, so your view gets spliced in inside the HTML body, and your partial will then get spliced in at the appropriate point within the view. Earlier in this thread you wrote: > as for why line 28...it''s because of all the html setup code <html><meta > tags><link tags to javascripts><style tags><body><table><tbody><tr><td> > stuff - each ''partial'' actually becomes a fully independent ''page'' as > there is a div with a page break at the bottom before the closing > </html> tag as I am iterating each ''facility'' into a separately printed > document. What does this mean? What are you expecting to return to the browser? Are you confusing HTML pages with printed pages? I''d like to reinforce the advice others have given you. The definitive documentation is the API RDoc. I was impressed when I first followed "Four Days on Rails" about a year ago with the way it introduced each feature and then showed where to look in the API documentation for more information. And while AWDR was published before Rails reached 1.0, the core team committed to retain compatibility with the book. regards Justin
On Sun, 2006-03-19 at 06:58 +0000, Justin Forder wrote:> Craig, > > Two points: > > 1. Learn how to use a feature on a trivial example before trying to > weave its use into your apparently complex application. I suggest you > keep a separate ''sandbox'' Rails application for trying things out. Had > you done this, you should have arrived at the correct understanding of > the AWDR description of partials before getting bogged down in their use > in your application - or, failing that, you would have had a small > focused example to discuss on the list. (Also, if you are not already in > the habit, use irb to try out Ruby features and use script/console to > test fragments of intended controller functionality against your models.) > > 2. How on earth can you have all this HTML boilerplate in a partial? > > > _log_page.rhtml > > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > > > > <html xmlns="http://www.w3.org/1999/xhtml"> > > <head> > > </head> > > <body> > > <%# session[:facility_log] = log_pages %> > > <%=h (log_page[:facility][:name]) %> > > </body> > > </html> > > You can put all the non-content stuff in a layout, so your view gets > spliced in inside the HTML body, and your partial will then get spliced > in at the appropriate point within the view. > > Earlier in this thread you wrote: > > > as for why line 28...it''s because of all the html setup code <html><meta > > tags><link tags to javascripts><style tags><body><table><tbody><tr><td> > > stuff - each ''partial'' actually becomes a fully independent ''page'' as > > there is a div with a page break at the bottom before the closing > > </html> tag as I am iterating each ''facility'' into a separately printed > > document. > > What does this mean? What are you expecting to return to the browser? > Are you confusing HTML pages with printed pages? > > I''d like to reinforce the advice others have given you. The definitive > documentation is the API RDoc. I was impressed when I first followed > "Four Days on Rails" about a year ago with the way it introduced each > feature and then showed where to look in the API documentation for more > information. And while AWDR was published before Rails reached 1.0, the > core team committed to retain compatibility with the book. >---- yes, yes and yes... I have it all figured out and I will sort through it all tomorrow and get a wiki page up in the next few days. The issue though isn''t html code in partials, which of course is butt ugly and this report is intended to be a multi-page print and there are css page breaks, etc. The issue is simply the methodology of a complicated array of hashes within hashes and render :partial :collection just can''t deal with that. I ended up putting all of the code in a class and simply looping within the view code...it''s clean it''s neat it works and it avoids render :partial :collection which clearly works in simple cases and simply gags in multi-tiered arrays/hashes. I think that is why most experienced programmers haven''t run into this before...because they were already putting methods in their models that created the containers of data as objects where in my linear thinking, I tried to just accumulate all the data in a massive array in the controller and have view code sort it all out. I hope that makes sense to others as it seems to make sense to me. I have no experience with object oriented programming and it showed here. Thanks Craig
Craig White wrote:> On Sun, 2006-03-19 at 06:58 +0000, Justin Forder wrote:[...]>> 2. How on earth can you have all this HTML boilerplate in a partial? >> >>> _log_page.rhtml >>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> >>> >>> <html xmlns="http://www.w3.org/1999/xhtml"> >>> <head> >>> </head> >>> <body> >>> <%# session[:facility_log] = log_pages %> >>> <%=h (log_page[:facility][:name]) %> >>> </body> >>> </html> >> You can put all the non-content stuff in a layout, so your view gets >> spliced in inside the HTML body, and your partial will then get spliced >> in at the appropriate point within the view. >> >> Earlier in this thread you wrote: >> >> > as for why line 28...it''s because of all the html setup code <html><meta >> > tags><link tags to javascripts><style tags><body><table><tbody><tr><td> >> > stuff - each ''partial'' actually becomes a fully independent ''page'' as >> > there is a div with a page break at the bottom before the closing >> > </html> tag as I am iterating each ''facility'' into a separately printed >> > document. >> >> What does this mean? What are you expecting to return to the browser? >> Are you confusing HTML pages with printed pages? >> >> I''d like to reinforce the advice others have given you. The definitive >> documentation is the API RDoc. I was impressed when I first followed >> "Four Days on Rails" about a year ago with the way it introduced each >> feature and then showed where to look in the API documentation for more >> information. And while AWDR was published before Rails reached 1.0, the >> core team committed to retain compatibility with the book. >> > ---- > yes, yes and yes... > > I have it all figured out and I will sort through it all tomorrow and > get a wiki page up in the next few days. > > The issue though isn''t html code in partials, which of course is butt > ugly and this report is intended to be a multi-page print and there are > css page breaks, etc.It *does* sound as if you are confusing HTML pages (more accurately, HTML documents) with printed pages. Your HTML response must only contain one <html> element. CSS page breaks give some control over how it is rendered for print.> The issue is simply the methodology of a complicated array of hashes > within hashes and render :partial :collection just can''t deal with that.You appear to still believe that you ran into a bug in Rails. Try to develop a minimal demonstration of this, and then if you are still convinced there is something wrong, discuss it again.> I ended up putting all of the code in a class and simply looping within > the view code...it''s clean it''s neat it works and it avoids > render :partial :collection which clearly works in simple cases and > simply gags in multi-tiered arrays/hashes.Please demonstrate this rather than continuing to assert it without supporting evidence.> I think that is why most experienced programmers haven''t run into this > before...because they were already putting methods in their models that > created the containers of data as objects where in my linear thinking, I > tried to just accumulate all the data in a massive array in the > controller and have view code sort it all out. I hope that makes sense > to others as it seems to make sense to me. I have no experience with > object oriented programming and it showed here.Don''t forget that the arrays and hashes you were using are objects too. regards Justin
On Sun, 2006-03-19 at 14:36 +0000, Justin Forder wrote:> Craig White wrote: > > On Sun, 2006-03-19 at 06:58 +0000, Justin Forder wrote: > [...] > >> 2. How on earth can you have all this HTML boilerplate in a partial? > >> > >>> _log_page.rhtml > >>> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> > >>> > >>> <html xmlns="http://www.w3.org/1999/xhtml"> > >>> <head> > >>> </head> > >>> <body> > >>> <%# session[:facility_log] = log_pages %> > >>> <%=h (log_page[:facility][:name]) %> > >>> </body> > >>> </html> > >> You can put all the non-content stuff in a layout, so your view gets > >> spliced in inside the HTML body, and your partial will then get spliced > >> in at the appropriate point within the view. > >> > >> Earlier in this thread you wrote: > >> > >> > as for why line 28...it''s because of all the html setup code <html><meta > >> > tags><link tags to javascripts><style tags><body><table><tbody><tr><td> > >> > stuff - each ''partial'' actually becomes a fully independent ''page'' as > >> > there is a div with a page break at the bottom before the closing > >> > </html> tag as I am iterating each ''facility'' into a separately printed > >> > document. > >> > >> What does this mean? What are you expecting to return to the browser? > >> Are you confusing HTML pages with printed pages? > >> > >> I''d like to reinforce the advice others have given you. The definitive > >> documentation is the API RDoc. I was impressed when I first followed > >> "Four Days on Rails" about a year ago with the way it introduced each > >> feature and then showed where to look in the API documentation for more > >> information. And while AWDR was published before Rails reached 1.0, the > >> core team committed to retain compatibility with the book. > >> > > ---- > > yes, yes and yes... > > > > I have it all figured out and I will sort through it all tomorrow and > > get a wiki page up in the next few days. > > > > The issue though isn''t html code in partials, which of course is butt > > ugly and this report is intended to be a multi-page print and there are > > css page breaks, etc. > > It *does* sound as if you are confusing HTML pages (more accurately, > HTML documents) with printed pages. Your HTML response must only contain > one <html> element. CSS page breaks give some control over how it is > rendered for print. > > > The issue is simply the methodology of a complicated array of hashes > > within hashes and render :partial :collection just can''t deal with that. > > You appear to still believe that you ran into a bug in Rails. Try to > develop a minimal demonstration of this, and then if you are still > convinced there is something wrong, discuss it again. > > > I ended up putting all of the code in a class and simply looping within > > the view code...it''s clean it''s neat it works and it avoids > > render :partial :collection which clearly works in simple cases and > > simply gags in multi-tiered arrays/hashes. > > Please demonstrate this rather than continuing to assert it without > supporting evidence. > > > I think that is why most experienced programmers haven''t run into this > > before...because they were already putting methods in their models that > > created the containers of data as objects where in my linear thinking, I > > tried to just accumulate all the data in a massive array in the > > controller and have view code sort it all out. I hope that makes sense > > to others as it seems to make sense to me. I have no experience with > > object oriented programming and it showed here. > > Don''t forget that the arrays and hashes you were using are objects too.---- OK - at some point, I will try to create some exhibition type structure for someone to use to examine my contention but that is no simple task and given the incredible amount of wasted hours that I have on this, I am so far behind in real work that it is going to wait simply out of necessity. Craig