轨道3:API PUT请求失败,不更新记录 [英] Rails 3: API PUT request fails, record not updated

查看:132
本文介绍了轨道3:API PUT请求失败,不更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与API的应用程序更新艺术家和试图与API交互更新艺术家的客户端。问题是,每当我试图做一个PUT请求,记录不更新,请求失败了已完成204在14毫秒无内容(ActiveRecord的:0.0ms)错误

I have an application with an API to update artists and a client that tries to interact with the API to update an artist. The problem is that whenever I try to do a PUT request, the record is not updated and the request fails with a Completed 204 No Content in 14ms (ActiveRecord: 0.0ms) error.

下面是API控制器:

class Api::V1::ArtistsController < Api::V1::BaseController
  respond_to :json

  def show
    respond_with Artist.find_by_id(params[:id])
  end

  def update
    artist = Artist.find_by_id(params[:id])
    respond_with artist.update_attributes(params[:artist])
  end
end

这使得API调用从客户端模型的方法:

The method that makes the API call from the client model:

def update_artist_attributes
  self.class.put("/api/artists/#{self.artist_id}.json", { body: {
    artist: {
      bio: self.artist_attributes_copy[:bio],
      phone_number: self.artist_attributes_copy[:phone_number],
      country: self.artist_attributes_copy[:country],
      city: self.artist_attributes_copy[:city]
    }
  } })
end

和服务器日志(API侧):

And the server logs (API side):

Started PUT "/api/artists/1.json" for 127.0.0.1 at 2013-02-02 19:00:00 +0100
Processing by Api::V1::ArtistsController#update as JSON
  Parameters: {"artist"=>{"bio"=>"NERVO sisters are not lesbians and they're hot! And they're single too!", "phone_number"=>"218391", "country"=>"Afghanistan", "city"=>"dnajksakd"}, "id"=>"1"}
  Artist Load (0.4ms)  SELECT "artists".* FROM "artists" WHERE "artists"."id" = 1 LIMIT 1
   (0.2ms)  BEGIN
  Artist Exists (0.4ms)  SELECT 1 AS one FROM "artists" WHERE "artists"."access_token" = 'EqHG8SGh9ldl3W-U5PBECw' LIMIT 1
  Artist Exists (0.6ms)  SELECT 1 AS one FROM "artists" WHERE (LOWER("artists"."access_token") = LOWER('EqHG8SGh9ldl3W-U5PBECw') AND "artists"."id" != 1) LIMIT 1
  Artist Exists (0.5ms)  SELECT 1 AS one FROM "artists" WHERE ("artists"."email" IS NULL AND "artists"."id" != 1) LIMIT 1
   (0.3ms)  ROLLBACK
Completed 204 No Content in 14ms (ActiveRecord: 0.0ms)

我在做什么错了?

What am I doing wrong?

推荐答案

在204发生问题,因为 update_attributes方法不返回模型实例。您希望您的更新方法是:

The 204 problem is occurring because update_attributes does not return the model instance. You want your update method to be:

artist = Artist.find_by_id(params[:id])
artist.update_attributes(params[:artist])
respond_with artist

这篇关于轨道3:API PUT请求失败,不更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆