更新Rails控制器中的属性



我试图更新用户的shop_id时,他们点击一个添加按钮。

被调用的控制器方法是:

def update_shop
  @user = User.find(params[:id])
  @shop = Shop.find(params[:shop_id])
  @user.update_attribute(:shop_id, params[:shop_id])
  flash[:success] = "Added Shop!"
  redirect_to @shop
end

按钮被点击时服务器读取:

Started POST "/updateshop?id=3&shop_id=1" for ::1 at 2015-08-31 05:50:52 -0500
Processing by UsersController#update_shop as HTML
  Parameters: {"authenticity_token"=>"jRozldw1u3TrWhaL6CeJyw4Tm5V5S/IFEQQulRkuV1Ot85kmPOsMa2jH2L6m8EFDpy7Ygc9SMBvPLJCuosHXUg==", "id"=>"3", "shop_id"=>"1"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
  Shop Load (0.1ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  UPDATE "users" SET "shop_id" = ?, "updated_at" = ? WHERE "users"."id" = ?  [["shop_id", 1], ["updated_at", "2015-08-31 10:50:52.089826"], ["id", 3]]
   (5.8ms)  commit transaction

但它实际上并不更新User.shop_id有时它没有更新行,读取为:

Started POST "/updateshop?id=3&shop_id=1" for ::1 at 2015-08-31 05:56:54 -0500
Processing by UsersController#update_shop as HTML
  Parameters: {"authenticity_token"=>"D1ODlfDnhJmQ9NUfh+GL2JE747nJC2t4eqOziRGNCaUvuikmEDkzhhNpGyrJNkNQOAagrX8SqWakiw2yqmKJpA==", "id"=>"3", "shop_id"=>"1"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 3]]
  Shop Load (0.1ms)  SELECT  "shops".* FROM "shops" WHERE "shops"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
   (0.1ms)  commit transaction

参数传递正确,我认为update_attribute是正确的,出了什么问题?

值已经分配,这就是为什么它没有更新。我的意思是,没有实际的变化,这就是为什么没有发出UPDATE语句。

相关内容

  • 没有找到相关文章

最新更新