Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Api错误针对Rails 3的自定义,如Github Api v3_Ruby_Ruby On Rails 3_Api - Fatal编程技术网

Ruby Api错误针对Rails 3的自定义,如Github Api v3

Ruby Api错误针对Rails 3的自定义,如Github Api v3,ruby,ruby-on-rails-3,api,Ruby,Ruby On Rails 3,Api,我在Rails3应用程序上添加了一个API,它运行得非常好。 但是我在v3上看到了下面的Github api 我喜欢错误消息结构。但无法让它繁殖。 如何使我的API使响应像这样?通过为JSON格式添加ActionController::Responder,您可以很容易地实现该错误格式。有关此类的文档(非常模糊),请参见,但简而言之,您需要重写to_json方法 在下面的示例中,我在ActionController:Responder中调用一个私有方法,它将构造json响应,包括您选择的自定义错误

我在Rails3应用程序上添加了一个API,它运行得非常好。 但是我在v3上看到了下面的Github api

我喜欢错误消息结构。但无法让它繁殖。
如何使我的API使响应像这样?

通过为JSON格式添加ActionController::Responder,您可以很容易地实现该错误格式。有关此类的文档(非常模糊),请参见,但简而言之,您需要重写to_json方法

在下面的示例中,我在ActionController:Responder中调用一个私有方法,它将构造json响应,包括您选择的自定义错误响应;你所要做的就是填补空白,真的:

def to_json
  json, status = response_data
  render :json => json, :status => status
end

def response_data
  status = options[:status] || 200
  message = options[:notice] || ''
  data = options[:data] || []

  if data.blank? && !resource.blank?
    if has_errors?
      # Do whatever you need to your response to make this happen.
      # You'll generally just want to munge resource.errors here into the format you want.
    else
      # Do something here for other types of responses.
    end
  end

  hash_for_json = { :data => data, :message => message }

  [hash_for_json, status]
end

他也有同样的问题。你解决了吗?我通过创建自定义异常类解决了。你还可以在这个自定义异常类中使用内置rails验证吗?你没有写博客吗?@MarkMurphy我们把它放在
lib
目录下。看看哪个能证实这一点。
def to_json
  json, status = response_data
  render :json => json, :status => status
end

def response_data
  status = options[:status] || 200
  message = options[:notice] || ''
  data = options[:data] || []

  if data.blank? && !resource.blank?
    if has_errors?
      # Do whatever you need to your response to make this happen.
      # You'll generally just want to munge resource.errors here into the format you want.
    else
      # Do something here for other types of responses.
    end
  end

  hash_for_json = { :data => data, :message => message }

  [hash_for_json, status]
end