views:

34

answers:

2

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 protected]"}

User Load (0.6ms) SELECT * FROM "users" WHERE ("users"."email" = '[email protected]') LIMIT 1 Completed in 20ms (View: 1, DB: 1) | 200 OK [http://localhost/users/updateName]

But when I check my database, the name field is never updated. What am I doing wrong? Thanks for reading.

+2  A: 

You need to call update method of your model after you update the fields

@user.update()
Snehal
+2  A: 

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.

Slobodan Kovacevic
you could also use `@user.update_attribute(:name, params[:nameNew])` which updates the attribute and saves the record immediately. Especially useful in a case like this :)
nathanvda