【发布时间】:2011-11-08 04:19:29
【问题描述】:
在 Rails 3.1 应用程序中,我有一个控制器使用以下代码在索引视图中返回一组对象(子对象):
def index
@children = Child.all
@base_class = "children-index"
@title = "Your Children"
respond_to do |format|
format.html # children/index.html.erb
format.json { render :json => @children }
end
end
index.html.erb 视图是这样写的:
<h1><%= title %></h1>
<ul>
<%= @children.each do |child| %>
<li><%= link_to child.fullname, child_path(child) %></li>
<% end %>
</ul>
由于某种原因,JSON 响应被扔进了 HTML 响应中,我无法确定原因。我的其他索引视图都没有这个问题,它们的代码非常接近。
John Jake Smith Jr
Jane Ann Doe
[#<Child id: 1, firstname: "John", middlename: "Jake", lastname: "Smith", suffix: "Jr", gender: "Male", dob_actual: "2011-01-05", dob_expected: "2011-01-01", height: 30, weight: 40, created_at: "2011-10-28 21:32:54", updated_at: "2011-10-28 21:32:54">, #<Child id: 2, firstname: "Jane", middlename: "Ann", lastname: "Doe", suffix: "", gender: "Female", dob_actual: "2011-05-05", dob_expected: "2011-05-01", height: 30, weight: 12, created_at: "2011-11-07 18:08:54", updated_at: "2011-11-07 18:08:54">]
【问题讨论】:
标签: ruby-on-rails ruby view controller