2013-02-17 66 views
0

我在“entries_controller.rb”下面的代码Rails的不的update_attributes更新记录

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条款被评估为真实的。查看“标题”的值没有从“主页”更改为“更新的主页”。

我想知道这是什么错误?它说它更新了,但并没有真正更新。

有人可以帮我理解这个吗?

感谢

+1

我有一个想法,它必须做你的形式。您可以发布您用于更新条目的表单的代码吗?这只是一种预感,但是你的表单不是以正确的方式发送参数,你的输入属性应该被包含在一个entry属性中。 – jason328 2013-02-17 20:35:29

+1

没有'params [:entry]',所以'update_attributes'什么都不做。 – Baldrick 2013-02-17 20:49:26

回答

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

+0

感谢您的回答。它现在有效 – banditKing 2013-02-18 00:59:22