I have a problem with rendering a partial. Despite my best efforts the
object I pass in using the :object attribute is valid just before I
call the [shared] partial, but somehow becomes nil inside the partial.
Background:
I have a “customer” object that includes an “address” model object by
aggregation. I render the address portion via a partial, so that I can
re-use addresses in other models.
Customer model:
composed_of :address,
:class_name => ‘Address’,
:mapping => [ # Database Ruby
[ :street, :street ],
…
[ :country_id, :country_id ]
]
Customer show.rhtml (with extra debug statement):
…
) %>
<%= h( @customer.address.street ) %>
<%= render( :partial => ‘/addresses/address_show’, :object =>
@customer.address )%>
…
/addresses/_address_show.rhtml: (The partial)
relevant action from the Customer controller:
def show
@customer = Customer.find(params[:id])
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @customer.to_xml } # Not using this
at the moment
end
end
I don’t have a variable called address_show anywhere in the controller
(I read another post that implied having the partial name match a
variable name in the controller might confuse things.)
The error message:
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.street
Extracted source (around line #3):
…
2:
3:
Can anyone explain what I’m doing wrong: why @customer.address appears
to be valid in one line of the customer view, but ends up in passing
nil to the partial in the next line?
Confused,
-Bruce