Problem with partial's object being nil

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):         ...   <tr>     <td><b>Website</b></td>     <td><%= link_to( @customer.website_url, :url => @customer.website_url ) %>   </tr>   <%= h( @customer.address.street ) %> <!-- This debug statement renders the customer's street address okay -->   <%= render( :partial => '/addresses/address_show', :object => @customer.address )%>         ...

/addresses/_address_show.rhtml: (The partial) <tr>   <td><b>Address:</b></td>   <td><%= h( @address_show.street ) %></td> <!-- I get an error attempting to access nil.street --> </tr>

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: <td><b>Address:</b></td> 3: <td><%= h( @address_show.street ) %></td>

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

Bruce wrote:

/addresses/_address_show.rhtml: (The partial) <tr>   <td><b>Address:</b></td>   <td><%= h( @address_show.street ) %></td> <!-- I get an error attempting to access nil.street --> </tr>

It should be: <%= h address_show.street %>

Dan Manges