对象字符串在Rails 4.1中打印到HTML流

时间:2014-04-17 18:13:57

标签: html ruby erb ruby-on-rails-4.1

我在Rails(4.1)中有几个简单的多态关系。 Address和Phone对象可以属于Person或Organization对象。所有的管道似乎都能正常工作 - CRUD操作都正常工作,我的所有测试都通过了。 Person和Organization的show动作包括一个应该显示相关地址和电话的部分动作。虽然这些字段确实正确显示,但整个Ruby对象也在HTML流中呈现为字符串,这当然是有问题的。 PeopleController的show动作:

def show
    @addressable = @person
    @addresses = @addressable.addresses

    @phoneable - @person
    @phones = @phoneable.phones
end

@person变量使用私有函数在before_action中设置:

def set_person
    @person = Person.find(params[:id])
end

show.html.erb for person(只是地址和电话部分):

<h3>Addresses</h3>
<%= render 'addresses/addresses' %.
<br>
<h3>Phones</h3>
<%= render 'phones/phones' %>

_address.html.erb部分(电话基本相同):

<%= @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 %>

HTML输出是这样的(再次,这只是地址和电话部分):

<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;]

如您所见,实际对象在流中呈现以及所需的HTML。 我在这里做错了什么想法?

1 个答案:

答案 0 :(得分:0)

each会返回您正在调用each的内容:

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

<%= ... %>用于插值,所以:

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

@addresses放入HTML中,以及奇怪的HTML来自哪里。

您只想与<% ... %>说明each

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

评估(不是插入each