2

列表显示

我有一个 Rails 视图,在这种情况下输出模型值的内容议程.主题

检查数据库,底部两项不在数据库中,我知道 :id 和空白是,但我的代码中的内容可能导致它像这样显示

用户/index.html.erb

<div class="row">
    <div class="span8">
        <%= render 'agendas/agenda_form' %>
        <% if @user.agendas.any? %>
            <h2>Agendas (<%= @user.agendas.count %>)</h2>
            <ol class="agendas">
                <%= render @agendas %>
            </ol>
        <% end %>
    </div>
</div>

the _agenda.html.erb partial
<li>
  <span class="content"><%= agenda.subject %></span>
  <%= link_to 'delete', agenda, method: :delete,
                                confirm: "You Sure?",
                                title: agenda.subject %>
</li>

和用户控制器

class UsersController < ApplicationController
  before_filter :authenticate_user!

  def index
    @user = current_user
    @agenda = current_user.agendas.build if signed_in?
    @agendas = current_user.agendas
  end
end

.... user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #] 8

[#<Agenda id: 49, subject: "Ut qui vel eos quia vitae.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #<Agenda id: 43, subject: "Enim dolorem.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #<Agenda id: 37, subject: "Rerum architecto est nihil totam.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #<Agenda id: 31, subject: "Non.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #<Agenda id: 25, subject: "Libero enim et explicabo.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #<Agenda id: 19, subject: "Et molestiae et quia saepe quia.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #<Agenda id: 7, subject: "Eum consectetur iste.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, #<Agenda id: nil, subject: nil, due_date: nil, completed: nil, user_id: 1, created_at: nil, updated_at: nil>] 8

我排除了前几条记录,它们看起来很正常,但最后两条记录很奇怪来自@agendas.inspect 和@agendas.length 的输出

我正在使用 faker 为应用程序创建示例数据,可能是罪魁祸首?

4

2 回答 2

1

谢谢 -

鉴于您的测试数据:

[#<Agenda id: 49, subject: "Ut qui vel eos quia vitae.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, 
#<Agenda id: 43, subject: "Enim dolorem.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, 
#<Agenda id: 37, subject: "Rerum architecto est nihil totam.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, 
#<Agenda id: 31, subject: "Non.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, 
#<Agenda id: 25, subject: "Libero enim et explicabo.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, 
#<Agenda id: 19, subject: "Et molestiae et quia saepe quia.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, 
#<Agenda id: 7, subject: "Eum consectetur iste.", due_date: nil, completed: nil, user_id: 1, created_at: "2012-05-17 15:52:22", updated_at: "2012-05-17 15:52:22">, 
#<Agenda id: nil, subject: nil, due_date: nil, completed: nil, user_id: 1, created_at: nil, updated_at: nil>]

看起来罪魁祸首就在数据中。我建议确认您实际上正在生成您认为的数据。

于 2012-05-17T17:47:36.223 回答
0

你在打电话

current_user.agendas.build current_user.agendas.build

每当登录用户查看此页面时,它将在议程列表中附加一个空白的未保存议程。从你的问题中不清楚你做什么,@agenda但你最好将它设置为Agenda.new

于 2012-05-17T17:56:04.713 回答