1

アーティストを更新する API を備えたアプリケーションと、API とやり取りしてアーティストを更新しようとするクライアントがあります。問題は、PUT リクエストを実行しようとすると、レコードが更新されず、リクエストがCompleted 204 No Content in 14ms (ActiveRecord: 0.0ms)エラーで失敗することです。

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 呼び出しを行うメソッド:

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 側):

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)

私は何を間違っていますか?

4

3 に答える 3

4

update_attributesモデルインスタンスを返さないため、204の問題が発生しています。更新方法は次のようにします。

artist = Artist.find_by_id(params[:id])
artist.update_attributes(params[:artist])
respond_with artist
于 2013-02-02T18:22:31.240 に答える
1

update_attributes操作の成功に応じてtrueまたはを返します。false

この結果をrespond_withに渡すので、これが 204 コード (「コンテンツなし」) を受け取る理由だと思います。 の結果が何であれ、リクエストは成功したと見なされupdate_attributesますが、コンテンツは返されません。

于 2013-02-02T18:27:11.067 に答える
0

提出された2つの回答は有効ですが、問題の核心は検証エラーがあったため、モデルオブジェクトが更新されなかったことです...

于 2013-02-02T18:29:27.593 に答える