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})
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With