Ruby on rails Rails 3:API PUT请求失败,记录未更新
我有一个带API的应用程序来更新艺术家,还有一个客户端试图与API交互来更新艺术家。问题是,每当我尝试执行PUT请求时,记录都不会更新,请求失败,在14毫秒内没有内容(ActiveRecord:0.0毫秒)错误 以下是API控制器:Ruby on rails Rails 3:API PUT请求失败,记录未更新,ruby-on-rails,api,put,Ruby On Rails,Api,Put,我有一个带API的应用程序来更新艺术家,还有一个客户端试图与API交互来更新艺术家。问题是,每当我尝试执行PUT请求时,记录都不会更新,请求失败,在14毫秒内没有内容(ActiveRecord:0.0毫秒)错误 以下是API控制器: class Api::V1::ArtistsController < Api::V1::BaseController respond_to :json def show respond_with Artist.find_by_id(param
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端):
我做错了什么?发生204问题是因为
update\u attributes
没有返回模型实例。您希望更新方法为:
artist = Artist.find_by_id(params[:id])
artist.update_attributes(params[:artist])
respond_with artist
发生204问题的原因是
update\u attributes
不返回模型实例。您希望更新方法为:
artist = Artist.find_by_id(params[:id])
artist.update_attributes(params[:artist])
respond_with artist
update\u attributes
根据操作的成功程度返回true
或false
您将此结果传递给
respond\u with
,因此我认为这就是您获得204代码(“无内容”)的原因-无论update\u attributes
的结果如何,请求都被视为成功,但不返回任何内容。update\u attributes
根据操作的成功程度返回true
或false
您将此结果传递给
response\u with
,因此我认为这就是您获得204代码(“无内容”)的原因-无论update\u attributes
的结果如何,请求都被视为成功,但不返回任何内容。提交的两个答案是有效的,但问题的核心是我有验证错误,所以模型对象没有得到更新…提交的两个答案是有效的,但问题的核心是我有验证错误,所以模型对象没有得到更新
artist = Artist.find_by_id(params[:id])
artist.update_attributes(params[:artist])
respond_with artist