5

ネストされた ActiveResource モデルがあります (つまり、別のモデルの名前空間内にあります)。saveレイズをコールしようとしています:

ActiveResource::MissingPrefixParam: client_id prefix_option is missing

必要なプレフィックスを指定するにはどうすればよいですか?

これが私のクラスです:

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

これが私の保存の試みです:

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

これについての説明を何度も探しましたが、同じ指示しか見つかりません。

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})

4

2 に答える 2

6

次のようにprefix_options、必要なハッシュを含むメソッドを作成すると機能します。client_id prefix_option

def prefix_options
  { :client_id => client_id }
end
于 2012-06-07T15:11:40.580 に答える
-1

特定のクライアント用に新しいポリシーを作成することを想定しています。ポリシーの詳細を投稿するパスは次のとおりです。

client_policies_path(@client.id)

フォームを使用している場合は、次のようにform_forを使用できます。

form_for [@client, @policy] do

ここで@client、および@policyは次のようにPolicyControllerのアクションによって作成され#newます。

def new
  @client = Client.new(params[:client_id])
  @policy = @client.policies.build
  # ...
end
于 2012-06-05T06:19:23.357 に答える