我的“entries_controller.rb”
中有以下代码 def update
@entry = Entry.find(params[:id])
puts "received HTTP request to update old entry:#{@entry.inspect} with"
puts "new parameters: #{params}"
if @entry.update_attributes(params[:entry])
puts"Update was successful"
@entry2 = Entry.find(params[:id])
puts "Here is the new value #{@entry2.inspect}"
end
end
当我从客户端向服务器发送put请求时,我的控制台显示:
2013-02-17T20:12:25+00:00 app[web.1]: received HTTP request to update old
entry:#<Entry id: 1, created_at: "2013-02-17 19:17:40", updated_at: "2013-02-17 19:17:40",
description: "Test", title: "Home", category: "-1", latitude: "49.258061", longitude: "-123.153972", hasPhoto: false> with
2013-02-17T20:12:25+00:00 app[web.1]: new parameters: {"description"=>"Test", "id"=>"1", "category"=>"-1", "hasPhoto"=>"0", "latitude"=>"49.258061", "longitude"=>"-123.153972",
"title"=>"Updated home", "action"=>"update", "controller"=>"entries", "format"=>"json"}
2013-02-17T20:12:25+00:00 app[web.1]: Update was successful
2013-02-17T20:12:25+00:00 app[web.1]: Here is the new value #<Entry id: 1, created_at: "2013-02-17 19:17:40", updated_at: "2013-02-17 19:17:40", description: "Test",
title: "Home", category: "-1", latitude: "49.258061", longitude: "-123.153972", hasPhoto: false>
正如您所看到的那样,旧记录并没有真正更新。即使if子句被评估为真。请参阅“标题”的值未从“主页”更改为“已更新主页”。
我想知道这个中的错误是什么?它说它更新但没有真正更新。
有人可以帮我理解这个吗?
由于
答案 0 :(得分:2)
在您的视图文件中,您没有在视图中设置相应的表单。您的参数正在返回
{"description"=>"Test", "id"=>"1", "category"=>"-1", "hasPhoto"=>"0", "latitude"=>"49.258061", "longitude"=>"-123.153972",
"title"=>"Updated home", "action"=>"update", "controller"=>"entries", "format"=>"json"}
实际上他们应该回来......
{"entry" => {"description"=>"Test", "id"=>"1", "category"=>"-1", "hasPhoto"=>"0", "latitude"=>"49.258061", "longitude"=>"-123.153972",
"title"=>"Updated home"}, "action"=>"update", "controller"=>"entries", "format"=>"json"}
注意区别?您的有关条目的参数将被包装并附加到属性条目。这样,当Rails查看您的参数时,它会知道哪些参数适用于您新更新的条目。没有看你的表格,没有办法告诉你如何具体修复它,但这里是start的好地方。