0

当我尝试这段代码时,它在 201 状态码的情况下工作......

resource_m = RestClient::Resource.new Rails.application.config.sta+"/mov"
resource_m = resource_m.post  @hash.to_json,  :content_type => :json, :accept =>:json
puts "code...#{resource_m.code}"

case resource_m.code
when 201
  respond_to do |format|
    @noticec =  {:name =>"Successfully",:code => "201"}
    format.json  { render :json => @noticec }
  end
when 409
  respond_to do |format|
    @noticec =  {:name =>"already exists",:code => "409" }
    format.json  { render :json => @noticec }
  end   

when 401
  respond_to do |format|
      @noticec =  {:name =>"Error",:code => "401" }
      format.json  { render :json => @noticec }
   end
else
  respond_to do |format|
      @noticec =  {:name =>"Something went wrong" }
      format.json  { render :json => @noticec }
   end

end

但是如果来自服务器的响应代码是 409,rails 给出一个错误 Completed 500 Internal Server Error in 1370ms ActionView::MissingTemplate (Missing template)。 但是当我在 REST-CLIENT 中尝试这个时,我可以看到状态码是 409 冲突。. 我无法在控制器中获取该响应代码。

4

1 回答 1

0

IIRCresource.post对 4xx、5xx 响应提出了异常。我对 ruby​​ 的 http 客户端很确定,对 RestClient 也很确定,但很容易修复

begin
  resource_m = resource_m.post  @hash.to_json,...
rescue 
  puts_or_debug
end
于 2013-06-26T14:54:12.113 回答