【问题标题】:Object string is printing to HTML stream in Rails 4.1对象字符串在 Rails 4.1 中打印到 HTML 流
【发布时间】:2014-04-18 03:05:34
【问题描述】:

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

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(只是地址和电话部分):

<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 一样在流中呈现。 关于我在这里做错了什么有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    each 返回你调用的东西 each 这样:

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

    而&lt;%= ... %&gt; 用于插值,因此:

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

    将 @addresses 放入 HTML 中,这就是奇怪的 HTML 的来源。

    你只想说&lt;% ... %&gt; 和each:

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

    评估(不是插值)each。

    【讨论】:

    • 感谢您的收获。盯着代码看了那么久,我完全错过了明显的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    相关资源
    最近更新 更多