Reputation: 9579
I have the following code in my "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
When I send a put request to the server from my client my console shows this:
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>
As you can see the old record was not really updated. Even though the if clause was evaluated to be true. See the value for "title" didn't change from "Home" to "Updated Home".
Im wondering whats the bug in this? Its saying that it updated but it didn't really update.
Can someone help me to understand this please?
Thanks
Upvotes: 1
Views: 2034
Reputation: 11107
What's happening is that within your view file you are not setting up the appropriate form in your view. Your parameters are returning as this
{"description"=>"Test", "id"=>"1", "category"=>"-1", "hasPhoto"=>"0", "latitude"=>"49.258061", "longitude"=>"-123.153972",
"title"=>"Updated home", "action"=>"update", "controller"=>"entries", "format"=>"json"}
When in reality they should be returning as this...
{"entry" => {"description"=>"Test", "id"=>"1", "category"=>"-1", "hasPhoto"=>"0", "latitude"=>"49.258061", "longitude"=>"-123.153972",
"title"=>"Updated home"}, "action"=>"update", "controller"=>"entries", "format"=>"json"}
Notice the difference? Your parameters regarding entry are wrapped up and attached to the attribute entry. That way when Rails looks at your parameters, it knows which parameters are for your newly updated entry. Without looking at your forms, there isn't a way to tell you how to specifically fix it, but here's a good place to start.
Upvotes: 2