我有从数据库中获取的对象集合,当我使用.each方法对其进行迭代时,我得到的单个对象只有空值,如下所示:
- !ruby/object:List
attributes:
id: 4
title: Test
project_id: 10
created_at: 2013-12-29 20:53:04.087839000 Z
updated_at: 2013-12-29 20:53:04.087839000 Z
- !ruby/object:List
attributes:
id: 5
title: Testng
project_id: 10
created_at: 2013-12-29 20:53:59.087687000 Z
updated_at: 2013-12-29 20:53:59.087687000 Z
- !ruby/object:List
attributes:
id:
title:
project_id: 10
created_at:
updated_at:
什么是好的,但是...在我的SQLite3数据库中我只有两行......因为在这里工作对我来说没有任何问题我想了解它为什么会发生。你能解释一下吗?
这是我的控制器和局部视图,其中我正在使用@current_lists变量
projects_controller.rb
def show
@project = Project.find(params[:id])
@attachment = Attachment.new
@list = @project.lists.new
@current_lists = @project.lists
@current_attachments = @project.attachments
@current_issues = @project.issues.includes(:category)
respond_to do |format|
format.html # show.html.erb
format.json { render json: @project }
end
end
_lists.html.haml
- @current_lists.each do |project|
.col-md-4
.panel.panel-default
.panel-heading
=project.title
.pull-right.text-success
%a.fa.fa-plus.fa-lg
%ul.list-group.task-list
%li.list-group-item.selected
%label.label-checkbox.inline
%input.task-finish{checked: "checked", type: "checkbox"}/
%span.custom-checkbox
SEO Optimisation
%span.pull-right
%a.task-del{href: "#"}
%i.fa.fa-trash-o.fa-lg.text-danger
/ /list-group
编辑: 这是我用来渲染使用@list实例变量的表单的模态代码:
/ /Modal
#newList.modal.fade
.modal-dialog
.modal-content
.modal-header
%button.close{"aria-hidden" => "true", "data-dismiss" => "modal", type: "button"} ×
%h4 Modal with form
.modal-body
=form_for [@project, @list] do |f|
- if @list.errors.any?
#error_explanation
%h2= "#{pluralize(@list.errors.count, "error")} prohibited this list from being saved:"
%ul
- @list.errors.full_messages.each do |msg|
%li= msg
.form-group
= f.label :title
= f.text_field :title, class: 'form-control input-sm'
.form-group
= f.submit 'Save', class: '.btn.btn-success'
/ /.modal-content
/ /.modal-dialog
/ /.modal
提前谢谢!
答案 0 :(得分:1)
您正在使用代码
在项目控制器中创建一个空列表对象@list = @project.lists.new
解决方案是仅从@project.lists
选择已保存的对象:
@current_lists = @project.lists.to_a.select { |list| !list.new_record? }
这将从@current_lists
数组中删除未保存的记录。
已修改为包含解决方案