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:
26: FACILITY:
27:
28: <%=
fac_log_test[fac_log_test_counter][:facility][:name] %>