Ruby on rails 无法处理rails 3中的409响应代码
当我尝试这段代码时,它在201状态代码的情况下工作Ruby on rails 无法处理rails 3中的409响应代码,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.2,当我尝试这段代码时,它在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 whe
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将给出一个错误
1370ms内发生服务器错误
ActionView::缺少模板(缺少模板)。
但是当我在REST-CLIENT中尝试此操作时,我可以看到状态代码是409冲突。。我无法在控制器中获取该响应代码。IIRC
resource.post
在4xx、5xx响应上引发异常。我对ruby的http客户机和RestClient都很确定,但它很容易修复
我尝试过begin rescue,现在我在rescue rescue=>e puts“error…”.{e.message}“end error…”409冲突中得到了代码
begin
resource_m = resource_m.post @hash.to_json,...
rescue
puts_or_debug
end