Question

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>
[#&lt;Address id: 2, address_line_1: &quot;1 Viceroy Plaza&quot;, address_line_2: &quot;Suite 1300&quot;, address_line_3: &quot;Office 125&quot;, address_line_4: nil, city: &quot;Toronto&quot;, postal_code: &quot;C8R 9G3&quot;, verified: false, use_for_contact: true, picture_file_name: nil, addressable_id: 1, addressable_type: &quot;Person&quot;, created_at: &quot;2014-04-13 20:32:41&quot;, updated_at: &quot;2014-04-13 20:32:41&quot;&gt;, #&lt;Address id: 1, address_line_1: &quot;1313 Mockingbird Lane&quot;, address_line_2: &quot;Apt. 227&quot;, address_line_3: nil, address_line_4: nil, city: &quot;Mississauga&quot;, postal_code: &quot;H3V 8Y4&quot;, verified: false, use_for_contact: true, picture_file_name: nil, addressable_id: 1, addressable_type: &quot;Person&quot;, created_at: &quot;2014-04-13 20:18:56&quot;, updated_at: &quot;2014-04-13 20:18:56&quot;&gt;]
  </br>
  <h3>Phones</h3>

  (1) 416-3219700<br><br>

  (1) 289-4346789<br><br>
[#&lt;Phone id: 2, country_code: &quot;1&quot;, area_code: &quot;416&quot;, phone_number: &quot;3219700&quot;, phone_extension: &quot;13125&quot;, verified: nil, use_for_contact: nil, phoneable_id: 1, phoneable_type: &quot;Person&quot;, created_at: &quot;2014-04-13 20:34:54&quot;, updated_at: &quot;2014-04-13 20:34:54&quot;&gt;, #&lt;Phone id: 1, country_code: &quot;1&quot;, area_code: &quot;289&quot;, phone_number: &quot;4346789&quot;, phone_extension: nil, verified: nil, use_for_contact: nil, phoneable_id: 1, phoneable_type: &quot;Person&quot;, created_at: &quot;2014-04-13 20:30:54&quot;, updated_at: &quot;2014-04-13 20:30:54&quot;&gt;]

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?

Était-ce utile?

La solution

each returns the thing you're calling each on so:

@addresses.each { ... } == @addresses

And <%= ... %> is for interpolation so this:

<%= @addresses.each do |address| %>

puts @addresses in the HTML and that's where the odd looking HTML is coming from.

You just want to say <% ... %> with each:

<% @addresses.each do |address| %>

to evaluate (not interpolate) the each.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top