Problem updating a database field from my controller
I have an update method in my users controller that I call from a HTTPService in Flex 4. The update method is as follows:
def updateName
@user = User.find_by_email(params[:email])
@user.name = params[:nameNew]
render :nothing => true
end
This is console output:
Processing UsersController#updateName (for 127.0.0.1 at 2010-05-24 14:12:49) [POST]
Parameters: {"action"=>"updateName", "nameNew"=>"ben", "controller"=>"users", "email"=>"email@gmail.com"}
User Load (0.6ms) SELECT * FROM "users" WHERE ("users"."email" = 'email@gmail.com') LIMIT 1 Completed in 20ms (View: 1, DB: 1) | 200 OK [http://localhost/users/updateName]
But when I check my database, the name field i开发者_开发知识库s never updated. What am I doing wrong? Thanks for reading.
You only modified user object in memory, i.e. you are not saving the change to database. You want something like:
def updateName
@user = User.find_by_email(params[:email])
@user.name = params[:nameNew]
@user.save
render :nothing => true
end
Additionally you might want to put @user.save in an if-condition to check if model was saved correctly.
You need to call update method of your model after you update the fields
@user.update()
精彩评论