Forum: Ruby on Rails Problem with partial's object being nil

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ddf1ea5e28fa76d5657fa0be7c9b08fe?d=identicon&s=25 Bruce (Guest)
on 2007-01-04 00:05
(Received via mailing list)
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.

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):
		<td><%= link_to( @customer.website_url, :url => @customer.website_url
) %>
	<%= 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)
	<td><%= h( @address_show.street ) %></td> <!-- I get an error
attempting to access nil.street -->

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

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?


B0f09f622c913a13b9bfd332fca56563?d=identicon&s=25 Dan Manges (Guest)
on 2007-01-04 04:45
(Received via mailing list)
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
This topic is locked and can not be replied to.