0

と呼ばれるモデルがあります。これは、やContributorなどの他のいくつかのモデルの名前空間としても機能します。次のようなコントリビューターIDを含むURLを使用したい:Contributor::AliasContributor::Reassignment

/contributors/1/reassignments/new

しかし、私はこのエラーを受け取ります:

No route matches [GET] "/contributors/1/reassignments/new"

私のroutes.rbファイルには以下が含まれます:

namespace :contributor do
  resources :reassignments
end
resources :contributors

私も試しました:

resources :contributors do
  resources :reassignments
end

これにより、別のエラーが発生します。

uninitialized constant ReassignmentsController

これにアプローチする方法はありますか?おそらく、モデルとしても機能する名前空間を使用するべきではありませんか?可能であるように思われますが、私はこれがどのチュートリアルでも行われているのを見たことがありません。

アップデート:

次のような、深くネストされた名前空間モデルをどのように処理しますか。

resources :contributors do
  resources :reassignments, :module => "contributor" do
    resources :approvals, :module => "reassignment"
  end
end

このアプローチを使用すると、次のエラーが発生します。

No route matches {:action=>"create", :controller=>"contributor/reassignment/approvals"}

私のコントローラディレクトリは次の構造を持っています:

contributor ->
  reassignment ->
    approvals_controller.rb

これは最初のエラーに関連しているようですが、おそらくそれは何か新しいものです。

4

1 に答える 1

1

コントリビューターリソースがあるかどうかは明確ではありません。その場合、routes.rbに次のように入力します。

resources :contributors do
  resources :reassignments, :module => "contributor"
end

そうでない場合は、次を試してください。

resources :reassignments, :module => "contributor", :path => "/contributors/:contributor_id/reassignments"

2番目のケースでは、URLを作成し、link_to、form_forなどの場所への呼び出しで明示的に:contributor_idを渡す必要があることに注意してください。

そこで[@contributor、@ reassignment]形式を使用する場合は、yuがContributorリソースを持っている最初のアプローチに固執する方がよいでしょう。

更新:コントローラーディレクトリがリソースと並行してネストしない場合の3レベルのネストの場合、コントローラーを明示的に指定できます。例:

resources :contributors do
  resources :reassignments, :controller => "contributor/reassignments" do
    resources :approvals, :controller => "reassignment/approvals"
  end
end  

しかし、そうしないでください。Railsでは、3レベル以上のネストは積極的に推奨されていません。代わりにここで推奨されるものを参照してください:http ://weblog.jamisbuck.org/2007/2/5/nesting-resources

于 2013-02-28T02:09:13.467 に答える