1

親の 1 つが名前空間化されたコントローラーであるポリモーフィックなネストされたリソースを使用すると、inherited_resources の問題に直面しています。抽象的な例を次に示します。

# routes.rb
resources :tasks do
  resources :comments
end   
namespace :admin do
  resources :projects do
    resources :comments
  end
end

# comments_controller.rb
class CommentsController < InheritedResources::Base
  belongs_to :projects, :tasks, :polymorphic => true
end

にアクセスする/admin/projects/1/commentsと、次のエラーが表示されます。

ActionController::RoutingError at /admin/projects/1/comments
uninitialized constant Admin::CommentsController

コントローラーを として定義するとAdmin::CommentsController、ファイルを移動する必要があり、その下controllers/adminにある URL に対してエラーがスローされます。/tasks/1/comments

これを修正する方法はありますか?

4

2 に答える 2

1

それをそのままにして、それを継承するCommentsController管理者用の別のコントローラーを作成してみませんか?admin/comments_controller.rb?

class Admin::CommentsController < CommentsController
   before_filter :do_some_admin_verification_stuff

  # since we're inheriting from CommentsController you'll be using
  # CommentsController's actions by default -  if you want
  # you can override them here with admin-specific stuff

protected
  def do_some_admin_verification_stuff
    # here you can check that your logged in used is indeed an admin,
    # otherwise you can redirect them somewhere safe.
  end
end
于 2013-12-18T11:24:04.393 に答える
0

あなたの質問に対する簡単な回答は、Rails Guideに記載されています。

基本的に、デフォルトがそこにないため、使用するコントローラーをルートマッパーに指示する必要があります。

#routes.rb
namespace :admin do
  resources :projects do
    resources :comments, controller: 'comments'
  end
end

これでルーティングの問題が解決されますが、これは実際にはおそらく とは関係ありませんInherited Resources

一方、Inherited Resources名前空間内にネストされたコントローラーの場合、同様に使用できませんでした。このため、私はその宝石から離れました。

私はあなたにとって興味深いものを作成しました:名前空間を説明する方法で、継承されたリソースが提供するすべての便利なルート ヘルパーを定義するコントローラーの懸念です。オプションの親子関係や複数の親子関係を処理できるほどスマートではありませんが、長いメソッド名を何度も入力する必要はありませんでした。

class Manage::UsersController < ApplicationController
  include RouteHelpers
  layout "manage"
  before_action :authenticate_admin!
  before_action :load_parent
  before_action :load_resource, only: [:show, :edit, :update, :destroy]
  respond_to :html, :js

  create_resource_helpers :manage, ::Account, ::User

  def index
    @users = parent.users
    respond_with [:manage, parent, @users]
  end

  def show
    respond_with resource_params
  end

  def new
    @user = parent.users.build
    respond_with resource_params
  end
  #  etc...
end

そして、私の見解の中で:

    td = link_to 'Show', resource_path(user)
    td = link_to 'Edit', edit_resource_path(user)
    td = link_to 'Destroy', resource_path(user), data: {:confirm => 'Are you sure?'}, :method => :delete

それが役立つことを願っています!

于 2014-05-15T21:43:33.723 に答える