2013-02-17 68 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