Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify prefix parameters when saving a nested ActiveResource?

I have a nested ActiveResource model (i.e. it's within another model's namespace). Trying to call save raises:

ActiveResource::MissingPrefixParam: client_id prefix_option is missing

How do I supply the needed prefix?

Here's my class:

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

Here's my save attempt:

bar = Foo::Bar.new :client_id => 123
bar.valid? # => true
bar.client_id # => 123
bar.save # => ActiveResource::MissingPrefixParam...

Time and again I've looked for elucidation on this, but I only find the same instruction:

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

I'm trying to access our server's API endpoint at http://www.example.com/clients/[client_id]/policies, but I'm apparently failing to supply client_id, so my app makes its request to http://www.example.com/clients//policies

The server log tells me: ActionController::RoutingError (No route matches "/clients//policies/" with {:method=>:post}).

like image 906
JellicleCat Avatar asked Jun 04 '12 18:06

JellicleCat


1 Answers

It will work if you create a method prefix_options, which supplies a Hash containing the needed client_id prefix_option, like so:

def prefix_options
  { :client_id => client_id }
end
like image 102
JellicleCat Avatar answered Oct 23 '22 22:10

JellicleCat