为什么我的迭代通过记录在一个陌生的地方添加整个对象转储?

问题描述 投票:1回答:1

我有两个型号,Habit和HabitLog。

为简单起见,Habit拥有一个名称并具有许多HabitLog。 HabitLog保存日期和状态。

我想为特定日期创建一个视图,并列出具有给定日期状态的所有习惯。

要获取记录,但保持关联,我在我的控制器中使用了此查询:

habit_log_controller.rb

def show
  @habit_logs = HabitLog.where(log_date: '2015-07-15').includes(:habit).order("habits.position")
end

然后在我看来,我有

show.html.erb

<ul>
  <%= @habit_logs.each do |habit_log| %>
    <li>
      <%= habit_log.habit.name + " " + habit_log.status %>
    </li>
  <% end %>
</ul>

它工作,意味着列出给定日期的所有习惯名称和状态,但它将整个数组粘贴在li标签之后的一个奇怪的地方,但是在ul内。

Page source

<ul>

  <li>
     Waga 87
  </li>

  <li>
     Higiena 1
  </li>

  <li>
     Siłownia 0
  </li>
  [#&lt;HabitLog id: 1, log_date: &quot;2015-07-15&quot;, status: &quot;87&quot;, habit_id: 1, created_at: &quot;2015-07-15 11:37:21&quot;, updated_at: &quot;2015-07-15 11:37:21&quot;&gt;, #&lt;HabitLog id: 2, log_date: &quot;2015-07-15&quot;, status: &quot;1&quot;, habit_id: 2, created_at: &quot;2015-07-15 11:37:28&quot;, updated_at: &quot;2015-07-15 11:37:28&quot;&gt;, #&lt;HabitLog id: 3, log_date: &quot;2015-07-15&quot;, status: &quot;0&quot;, habit_id: 3, created_at: &quot;2015-07-15 11:37:30&quot;, updated_at: &quot;2015-07-15 11:37:30&quot;&gt;]

</ul>

我认为这与我使用.includes(:habit)有关,但我不知道如何处理它。

ruby-on-rails ruby rails-activerecord active-record-query
1个回答
2
投票

问题是这一行

<%= @habit_logs.each do |habit_log| %>

应该

<% @habit_logs.each do |habit_log| %>

Imp注意:

<% %> # Executes the code.

<%= %> # Prints the output.
© www.soinside.com 2019 - 2024. All rights reserved.