I'm outputting a collection in haml
#- if @fields.count>0
.sfields
#= render :partial=>"sfields/field", :collection=>@fields, :as=>:field
= render @fields
When there are zero fields, there is always one phantom field rendered. When I add 'if @fields.count>0' then when there is zero there is no output, but as soon as I add one field I get two rendered, the one I added and a p开发者_C百科hantom field.
This is the first I've run into this and I'm not sure what I did that is outputting a phantom field. I remember seeing something similar, but can't remember where I saw it, and my search turned up nothing relevant atm.
EDIT 1:
Contents of partial
.sfield
= field.name
.fielditems
#- fields.items each do |i|
#= render :partial=>"items/item", :locals=>{:i => i}
= render field.items
So, there is always an extra sfield hanging around.
EDIT 2
Abstractly, I have this:
Show action -> partial for fields -> partial for field items -> partial for field item choices, 4 different places pulled into one page.
So fields is a collection of items and an item is a collection of choices. Even after going through and making the renders more rails default (e.g. render @fields, render field.items, etc.) I still have that phantom field. I don't want to hide it with js or something (ack awful), so I need to figure some solution which entails more trial and error and reading.
It works, but is ugly with the phantom field hanging on.
EDIT 3
I've taken several steps back combined everything into one template, sans partials. Still there. Something basic I'm doing wrong or missing is adding this phantom field, so annoying. It isn't necessarily the partials then.
EDIT 4: SOLVED
refined the @fields select in the controller to select only fields attached to the item I'm showing
e.g.
@fields = @showing.fields
to
@fields = @showing.fields(:showing_id=>@showing.id)
I'd still like to know why it was doing that grr...
Your @fields wouldn't be a list of hashes would it?
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.
-- http://api.rubyonrails.org/classes/ActionView/Partials.html
Note EDIT 4: SOLVED
refined the @fields select in the controller to select only fields attached to the item I'm showing
e.g.
@fields = @showing.fields
to
@fields = @showing.fields(:showing_id=>@showing.id)
精彩评论