POST到API后获取对象ID

时间:2015-01-27 16:51:21

标签: ruby-on-rails

我有一个连接到API的应用程序,允许您创建审核。创建新审核后,我需要重定向到该审核的“show”视图。如何检索新创建的审核的ID,以便将其传递给audits_path?这是我的代码:

audits_controller.rb:

def create
  payload = params.to_json
  rest_resource = RestClient::Resource.new("#{API_BASE_URL}/audits.json")
  begin
    rest_resource.post payload, :content_type => 'application/json'
    redirect_to audits_path(???)
  rescue Exception => e
    redirect_to audits_path
  end
end

1 个答案:

答案 0 :(得分:1)

有疑问请尝试分配deb = (rest_resource.post payload, :content_type => 'application/json')puts deb之类的变量,看看响应是否包含id,如果是,则传递给audits_path:redirect_to audits_path(id: xx)