2

我收到此错误:

Routing Error
uninitialized constant RegistrationsController

这是我的日志:

Started GET "/registrants/1/registrations/new" for 127.0.0.1 at 2012-07-03 00:55:44 -0500

ActionController::RoutingError (uninitialized constant RegistrationsController):

Rendered /.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (8.5ms)

这是我的 routes.rb 中合适的部分:

registrant_registrations GET    /registrants/:registrant_id/registrations(.:format)          {:action=>"index", :controller=>"registrations"}
                                   POST   /registrants/:registrant_id/registrations(.:format)          {:action=>"create", :controller=>"registrations"}
       new_registrant_registration GET    /registrants/:registrant_id/registrations/new(.:format)      {:action=>"new", :controller=>"registrations"}
      edit_registrant_registration GET    /registrants/:registrant_id/registrations/:id/edit(.:format) {:action=>"edit", :controller=>"registrations"}
           registrant_registration GET    /registrants/:registrant_id/registrations/:id(.:format)      {:action=>"show", :controller=>"registrations"}
                                   PUT    /registrants/:registrant_id/registrations/:id(.:format)      {:action=>"update", :controller=>"registrations"}

这是我的注册控制器,位于/app/controllers/portal/registrations_controller.rb

class Portal::RegistrationsController < ApplicationController
  def create
        @registrant = current_member.registrants.find(params[:registrant])
        @registration = @registrant.registrations.build

        respond_to do |format|
          if @registration.save
            format.html {   redirect_to root_path, :notice => "Registration successfully created!" }
            format.json { head :ok }
          else
           format.html { redirect_to root_path, :notice => "There was a problem with the registration." }
          end
        end     
  end

end

当我new_registrant_registration_path(registrant)Portal#index.

编辑:

为了完整起见,这是我在找到解决方案之前的完整 routes.rb 文件。雷达提到了我发布的要点,但我想我会把代码放在这里,以防要点被删除。

MyApp::Application.routes.draw do

  match 'admin' => 'admin/dashboard#index', :as => :admin_root
  match 'portal' => 'portal#index', :as => :member_root

  namespace 'admin' do
    match 'profile' => 'profile#show', :as => :profile

    resources :members
    resources :admins
        resources :packages
        resources :products
        resources :levels
        resources :lessons
        resources :registrations
        resources :registrants, :controller => 'customers/registrants'
        resources :locations
        resources :schools
        resources :terms
        resources :customers
        resources :invoices
        resources :payments
  end

  namespace 'member' do
    resources :registrations
  end

  match 'register' => 'member/registrations#new', :as => :signup  
  match 'login' => 'member_sessions#new', :as => :login
  match 'logout' => 'member_sessions#destroy', :as => :logout  
  match 'admin/login' => 'admin_sessions#new', :as => :admin_login
  match 'admin/logout' => 'admin_sessions#destroy', :as => :admin_logout

  match 'member/activate/:activation_code' => 'member/activations#create', :as => :member_activation
  match 'member/:id/activate/resend' => 'member/activations#resend', :as => :resend_member_activation
  match 'member/:id/activate' => 'member/activations#new', :as => :new_member_activation

  match 'member/password/reset/begin' => 'member/password_resets#new', :as => :new_member_password_reset, :via => :get
  match 'member/password/reset' => 'member/password_resets#create', :as => :member_password_resets, :via => :post
  match 'member/password/:token/finish' => 'member/password_resets#edit', :as => :edit_member_password_reset, :via => :get
  match 'member/password/reset' => 'member/password_resets#update', :as => :member_password_reset, :via => :put

  match 'admin/packages/new/:partial' => 'admin/packages#new'

  resources :admins

  resources :registrants, :controller => 'portal/registrants' do
    resources :registrations
  end

  resource :admin_session
  resource :member_session

  root :to => 'portal#index'

end
4

3 回答 3

10

说清楚一点:marcamillion 加入了#rubyonrails 和 Freenode 并分享了他的config/routes.rb. 这就是我找出他所犯的错误然后找出答案的地方。


两件事情。

第一:管理员根路由

而不是像这样为您的命名空间定义根路由admin

match 'admin' => 'admin/dashboard#index', :as => :admin_root

像这样在namespace :admin调用中定义它:

namespace 'admin' do
  root :to => "dashboard#index"
end

这将自动将其定义为admin_root_pathand admin_root_url,因此您不需要这样做,并且它会自动为控制器添加admin/前缀,因此您也不需要这样做。

二:引用RegistrantsController

您目前在您的config/routes.rb:

resources :registrants, :controller => 'portal/registrants' do
  resources :registrations
end

这将做的是定义资源路由以registrants正确使用Portal::RegistrantsController,但不会定义其下的嵌套路由 using Portal::RegistrationsController。它将尝试使用,这就是您收到该错误的原因RegistrationsController

要解决此问题,我建议您使用该scope方法,如下所示:

scope :module => Portal do
 resources :registrants do
  resources :registrations
 end
end

以这种方式使用该scope方法时,将告诉 Rails 块内路由的控制器位于Portal命名空间下。这意味着它将知道 that resources :registrantsis forPortal::Registrants和 that resources :registrationsis for Portal::Registrations,从而修复您看到的错误。

于 2012-07-03T06:59:48.083 回答
0

如果你改变这一行怎么办:

class Portal::RegistrationsController < ApplicationController

至:

class RegistrationsController < ApplicationController

?

于 2012-07-03T06:14:15.237 回答
0

这很奇怪。我认为,您的控制器应该位于/app/controllers/registrants/registrations_controller.rb并且看起来像这样:

class Registrants::RegistrationsController < Registrants::ApplicationController
  def create
    @registration = registrant.registrations.build

    ...
  end
end

class Registrants::ApplicationController < ApplicationController
  def registrant
    current_member.registrants.find(params[:registrant_id])
  end
end

Registrants::ApplicationController( /app/controllers/registrants/application_controller.rb) 控制器的目的是避免代码重复。

于 2012-07-03T06:21:38.833 回答