Question

I am facing an issue with inherited_resources when using a polymorphic nested resource, one of whose parents is a namespaced controller. Here is an abstract example:

# 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

When I access /admin/projects/1/comments, I get this error:

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

Now if I define the controller as Admin::CommentsController, I would need to move the file under controllers/admin which will in turn throw up an error for the url /tasks/1/comments

Is there a way I can fix this?

No correct solution

OTHER TIPS

Why not keep CommentsController where it is and make a separate controller for admin in admin/comments_controller.rb? that inherits from it?

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

The short answer to your question is mentioned here in the Rails Guide.

Basically you have to tell the route mapper which controller to use because the default is not there:

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

That will take care of your routing problem, which is actually probably not related to Inherited Resources.

On the other hand, I have been unable to use Inherited Resources as well, in cases of a nested controller inside a namespace. I've moved away from that gem because of this.

I created something that might be interesting to you: a controller concern that will define all of the useful route helpers that inherited resources gives, in a way that accounts for namespaces. It's not smart enough to handle optional or multiple parentage, but it spared me a lot of typing long method names.

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

And then inside my views:

    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

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top