I have a couple of simple polymorphic relations in Rails (4.1). The Address and Phone objects can belong to either a Person or an Organization object. All of the plumbing appears to work correctly - the CRUD operations are all working as they should and all my tests pass.
The show action for both Person and Organization includes a partial that is supposed to display the related Addresses and Phones. While those fields do display correctly, the entire Ruby object also renders as a string in the HTML stream, and that is of course problematic.
The show action of the PeopleController:
def show
@addressable = @person
@addresses = @addressable.addresses
@phoneable - @person
@phones = @phoneable.phones
end
The @person variable is set in a before_action using a private function:
def set_person
@person = Person.find(params[:id])
end
The show.html.erb for person (just the address and phone section):
<h3>Addresses</h3>
<%= render 'addresses/addresses' %.
<br>
<h3>Phones</h3>
<%= render 'phones/phones' %>
The _address.html.erb partial (the phone one is essentially identical):
<%= @addresses.each do |address| %>
<%= address.address_line_1 %><br>
<%= address.address_line_2 %><br>
<%= address.address_line_3 %><br>
<%= address.address_line_4 %><br>
<%= address.city + ', ' + address.postal_code %><br><br>
<% end %>
The HTML output is this (again, this is just the address and phone portion):
<h3>Addresses</h3>
1 Viceroy Plaza<br>
Suite 1300<br>
Office 125<br>
<br>
Toronto, C8R 9G3<br><br>
1313 Mockingbird Lane<br>
Apt. 227<br>
<br>
<br>
Mississauga, H3V 8Y4<br><br>
[#<Address id: 2, address_line_1: "1 Viceroy Plaza", address_line_2: "Suite 1300", address_line_3: "Office 125", address_line_4: nil, city: "Toronto", postal_code: "C8R 9G3", verified: false, use_for_contact: true, picture_file_name: nil, addressable_id: 1, addressable_type: "Person", created_at: "2014-04-13 20:32:41", updated_at: "2014-04-13 20:32:41">, #<Address id: 1, address_line_1: "1313 Mockingbird Lane", address_line_2: "Apt. 227", address_line_3: nil, address_line_4: nil, city: "Mississauga", postal_code: "H3V 8Y4", verified: false, use_for_contact: true, picture_file_name: nil, addressable_id: 1, addressable_type: "Person", created_at: "2014-04-13 20:18:56", updated_at: "2014-04-13 20:18:56">]
</br>
<h3>Phones</h3>
(1) 416-3219700<br><br>
(1) 289-4346789<br><br>
[#<Phone id: 2, country_code: "1", area_code: "416", phone_number: "3219700", phone_extension: "13125", verified: nil, use_for_contact: nil, phoneable_id: 1, phoneable_type: "Person", created_at: "2014-04-13 20:34:54", updated_at: "2014-04-13 20:34:54">, #<Phone id: 1, country_code: "1", area_code: "289", phone_number: "4346789", phone_extension: nil, verified: nil, use_for_contact: nil, phoneable_id: 1, phoneable_type: "Person", created_at: "2014-04-13 20:30:54", updated_at: "2014-04-13 20:30:54">]
As you can see, the actual objects are rendered in the stream as well as the desired HTML.
Any thoughts on what I am doing wrong here?