我在「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條款被評估爲真實的。查看「標題」的值沒有從「主頁」更改爲「更新的主頁」。
我想知道這是什麼錯誤?它說它更新了,但並沒有真正更新。
有人可以幫我理解這個嗎?
感謝
我有一個想法,它必須做你的形式。您可以發佈您用於更新條目的表單的代碼嗎?這只是一種預感,但是你的表單不是以正確的方式發送參數,你的輸入屬性應該被包含在一個entry屬性中。 – jason328 2013-02-17 20:35:29
沒有'params [:entry]',所以'update_attributes'什麼都不做。 – Baldrick 2013-02-17 20:49:26