2013-12-18 3 views
1

polymorphic 중첩 리소스를 사용할 때 inherited_resources 문제가 발생합니다. 부모 중 하나가 네임 스페이스 컨트롤러입니다. 내가 /admin/projects/1/comments에 액세스 할 때inherited_resources 'namespaced 컨트롤러가있는 polymorphic belongs_to를 사용할 수 없습니다.

# 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::CommentsController 같은 컨트롤러를 정의하면 이제

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

을, 나는 controllers/admin에서 파일을 이동해야합니다 그러면 URL에 오류가 발생합니다. /tasks/1/comments

해결 방법이 있습니까?

답변

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 
+0

감사합니다. 예, 이것은 하나의 해결책 이었지만,'inherited_resources'의 기본 목적 인'CommentsController'를 DRY 할 수 있는지 알아 내려고했습니다. 그렇지 않다면 나는 우려를 써서 두 주석 컨트롤러에 모두 포함시켜야 할 것이다. –

0

귀하의 질문에 짧은 대답은 Rails Guide 여기에 언급되어있다.

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

실제로 아마 Inherited Resources 관련이없는 라우팅 문제의 처리됩니다 :

은 기본적으로 당신은 기본이 없기 때문에 컨트롤러가 사용하는 경로 매퍼에게 있습니다.

반면에 네임 스페이스 안에 중첩 된 컨트롤러의 경우 Inherited Resources을 사용할 수 없습니다. 나는이 때문에 보석에서 물러났다.

당신이 흥미로울만한 것을 만들었습니다 : controller concern은 네임 스페이스를 설명하는 방식으로 상속 된 리소스가 제공하는 유용한 루트 도우미를 모두 정의합니다. 그것은 선택 적이거나 복수의 친자 관계를 처리 할만큼 똑똑하지 않지만, 그것은 긴 메소드 이름을 타이핑하는 것을 많이 저해했다. 내 견해 내부

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 

희망!