【问题标题】:cannot handle 409 response code in rails 3无法处理 Rails 3 中的 409 响应代码
【发布时间】:2013-06-26 14:46:55
【问题描述】:

当我尝试这段代码时,它在 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 1370 毫秒内的服务器错误 ActionView::MissingTemplate(缺少模板)。 但是当我在 REST-CLIENT 中尝试这个时,我可以看到状态码是 409 冲突。。我无法在控制器中获取该响应代码。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2


    【解决方案1】:

    IIRC resource.post 在 4xx、5xx 响应时引发异常。我对 ruby​​ 的 http 客户端很确定,对 RestClient 也很确定,但是很容易修复

    begin
      resource_m = resource_m.post  @hash.to_json,...
    rescue 
      puts_or_debug
    end
    

    【讨论】:

    • 我已经尝试过开始救援,现在我在救援救援中得到了该代码 => e puts "error.....#{e.message}" end error.....409冲突
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多