Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looks to me like it's a problem with the for-loops...</p> <p>In your example code, you are looping through the <code>@billings</code> (an array of billings objects passed from your controller), and calling the current one <code>billing</code>.</p> <p>In your form code, you are looping through the <code>:billings</code> (symbol referencing the billings ActiveRecords) and calling the current one <code>obj</code>. Then, confusingly, your code re-uses the variable name <code>obj</code> again in the @billings.collect function... but this should have local private scope and not effect your main loop, but it is hard to interpret on sight.</p> <p>Anyway, since the stuff inside your form loop doesn't reference <code>obj</code>, it is using the variable <code>billing</code>--which does not change when you loop through, but keeps the last value it had: the last item from the previous loop! This explains why you only see the last tag. (Also, the <code>debug(@billings)</code> function does its own looping through the records of sorts--it is not inside the fields_for do loop.)</p> <p>I would start with changing the first line of your form code to re-use the variable name <code>billing</code> again, like this:</p> <pre><code>&lt;% f.fields_for :billings do |billing| %&gt;&lt;br /&gt; </code></pre> <p><a href="http://rubylearning.com/satishtalim/ruby_blocks.html" rel="nofollow noreferrer">Ruby's block syntax</a> can be pretty confusing at first--or, heck, anytime you're reading code you didn't write yourself--but when you break it all down to the basic components, it's usually just looping through a set of items and performing some function on each one. Also watch out for the confusion between similar-looking names: @billing, @billings, :billings and billings are all different things to Ruby and Rails. Good luck! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload