保存嵌套的ActiveResource时如何指定前缀参数?

Jel*_*Cat 3 activeresource ruby-1.9.3 ruby-on-rails-3.2

我有一个嵌套的ActiveResource模型(即它在另一个模型的命名空间内).试图save加注:

ActiveResource::MissingPrefixParam: client_id prefix_option is missing
Run Code Online (Sandbox Code Playgroud)

我如何提供所需的前缀?

这是我的班级:

class Foo::Bar < ActiveResource::Base
  self.site = "http://www.example.com"
  self.prefix = "/clients/:client_id"
  self.element_name = "policy"
  self.collection_name = "policies"
end
Run Code Online (Sandbox Code Playgroud)

这是我的保存尝试:

bar = Foo::Bar.new :client_id => 123
bar.valid? # => true
bar.client_id # => 123
bar.save # => ActiveResource::MissingPrefixParam...
Run Code Online (Sandbox Code Playgroud)

我一次又一次地寻求阐明,但我只找到相同的指令:

When a GET is requested for a nested resource and you don’t provide the prefix_param an ActiveResource::MissingPrefixParam will be raised.

我正试图访问我们服务器的API端点http://www.example.com/clients/[client_id]/policies,但我显然无法提供client_id,所以我的应用程序发出请求http://www.example.com/clients//policies

服务器日志告诉我:ActionController::RoutingError (No route matches "/clients//policies/" with {:method=>:post}).

Jel*_*Cat 6

如果你创建一个方法prefix_options,它将提供包含所需的Hash ,它将起作用client_id prefix_option,如下所示:

def prefix_options
  { :client_id => client_id }
end
Run Code Online (Sandbox Code Playgroud)