2013-02-02 1 views
1

아티스트를 업데이트하는 API가있는 응용 프로그램과 아티스트를 업데이트하기 위해 API와 상호 작용하려는 클라이언트가 있습니다. 문제는 PUT 요청을 시도 할 때마다 레코드가 업데이트되지 않고 요청이 Completed 204 No Content in 14ms (ActiveRecord: 0.0ms) 오류로 실패한다는 것입니다. 여기 레일 3 : API PUT 요청이 실패하고 레코드가 업데이트되지 않습니다.

는 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

update_attributes이 모델 인스턴스를 반환하지 않기 때문에 204 문제가 발생합니다. 작업의 성공 여부에 따라

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

update_attributes 반환하거나 true 또는 false : 당신은 당신의 업데이트 방법이되고 싶어요.

이 결과는 respond_with으로 전달됩니다. 따라서 이것이 20437 코드 ("내용 없음")를 얻은 것입니다. 요청은 성공한 것으로 간주되어 update_attributes의 결과가 반환되지만 내용은 반환되지 않습니다.

0

제출 된 두 답변은 유효하지만 문제의 핵심은 유효성 검사 오류가있어서 모델 개체가 업데이트되지 않고 있다는 것입니다.