Rails:针对某些代码抑制输出

时间:2014-05-16 22:01:11

标签: ruby-on-rails

假设我的数组看起来像这样:

People => [
  {
    ID => /org/div/emp/123,
    Person => Jack,
    Age => 25
  },
  {
    ID => /org/div/emp/124,
    Person => Frank,
    Age => 45
  },
  {
    ID => /org/div/emp/125,
    Person => Molly,
    Age => 30
  }
]

我正在使用@People变量将此数组传递给我的视图。

我的观点现在很简单:

<h1>People</h1>
  <% @People.each do |person| %>
    <%= uri = person[ID].split("/") %>
    <p>
    Person: <%= person[Person] %> <br/>
    Age: <%= person[Age] %> <br/>
    ID: <%= uri[4] %>
    <br/>
    </p>
  <% end %>

问题在于,由于视图的工作方式,每当我分割人[ID]时,它都会显示为数组:

People

  ["org", "div", "emp", "123"]

  Person: Jack
  Age: 25
  ID: 123

  ["org", "div", "emp", "124"]

  Person: Frank
  Age: 45
  ID: 124

  ["org", "div", "emp", "125"]

  Person: Molly
  Age: 30
  ID: 125

如何在没有显示数组的情况下从URI获取ID?

1 个答案:

答案 0 :(得分:1)

请勿使用<%= uri = person[ID].split("/") %>代替<%- uri = person[ID].split("/") %>