这可能看起来像一个初学者的问题,但它让我难住。我有一个用户模型和一个位置模型。位置属于用户,并且用户具有多个位置。我有位置存储为我的路线文件中的嵌套资源。保存和检索模型
下面是我的LocationsController文件的创建功能:
def create
@user = User.find(params[:user_id])
@location = @user.locations.build(params[:location])
respond_to do |format|
if @location.save
format.html { redirect_to @user }
format.js {
@locations = @user.locations
render 'create'
}
else
format.html { render @user }
format.js
end
end
end
create.js.erb:
$('table#locations').html("<%= escape_javascript(render(@locations)) %>");
_location.html.erb:
<tr>
<td><%= location.user.name %></td>
<td><%= location.created_at %></td>
<td><%= location.longitude %></td>
<td><%= location.latitude %></td>
</tr>
保存到数据库使用AJAX工作没有问题。但是,检索记录时,返回表的最后一行只包含用户名。不显示created_at,经度和纬度。如果我刷新页面,则会显示它们。有没有人有任何想法,为什么这可能会发生?
谢谢。
更新
我颠倒了模型的秩序,它仍然是不打印底部入口。我的猜测是这是关于视图而不是模型。
这是一个很好的建议,但它没有奏效。无论如何感谢您的答复。 – LandonSchropp 2011-05-19 06:13:32
好吧,你可以将@locations的内容记录到控制器的控制台/日志中吗?你必须确定数据是否真的存在。如果是这样,那么你知道这是你的看法的问题(我怀疑你的视图代码看起来很简单)。 – CharlieMezak 2011-05-19 13:05:01
嗯,你是对的。当我从控制器记录数据时,最后一项缺少其数据。它读取#<位置id:nil,longitude:nil,纬度:nil,user_id:1,created_at:nil,updated_at:nil>。奇怪的部分是我在位置模型中验证经度和纬度的存在。 – LandonSchropp 2011-05-20 04:04:30