刚开始使用 Rails,我想使用具有以下端点的 Web 服务(使用 ActiveResource):
GET/user?some_header=XYZ
这是我的 ActiveResource 类:
class User < ActiveResource::Base
self.site = "url"
set_collection_name 'user' #avoid pluralization within the url
end
现在对上述端点的调用会是什么样子?
我试过了
User.get('', headers={:some_header => "XYZ"})
但是我收到了 404(当我手动触发该请求时,该请求有效)。
最佳答案
尝试
User.all(:params=>{:some_header => "XYZ"})
如果您想避免路径中的格式,您可以使用 ovveride 基 collection_path 方法
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}.#{format.extension}#{query_string(query_options)}"
end
至
def collection_path(prefix_options = {}, query_options = nil)
prefix_options, query_options = split_options(prefix_options) if query_options.nil?
"#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end
关于ruby-on-rails - Ruby/Rails - 通过 ActiveResource 使用 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9275595/