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。そこで私は彼が犯している誤りを理解し、それから答えを理解しました。


2つのこと。

最初:管理ルートルート

admin次のように名前空間のルートルートを定義するのではなく、次のようにします。

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

namespace :admin次のように呼び出し内で定義します。

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

admin_root_pathこれにより、自動的にandとして定義されるadmin_root_urlため、これを行う必要はありません。また、コントローラーにプレフィックスを自動的に付けるadmin/ため、これも行う必要はありません。

2番目:RegistrantsControllerの参照

あなたは現在あなたの中にこれを持っていますconfig/routes.rb

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

これにより、をregistrants正しく使用するためのリソースルートが定義されますが、を使用してこの下にネストされたルートは定義されません。を使用しようとするとそのエラーが発生しますPortal::RegistrantsControllerPortal::RegistrationsControllerRegistrationsController

これを修正するにはscope、次のような方法を使用することをお勧めします。

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

このscope方法をこのように使用すると、ブロック内のルートのコントローラーがPortal名前空間の下にあることがRailsに通知されます。これは、それがのためであり、それがのためresources :registrantsPortal::Registrantsあることがわかることを意味し、それによってあなたが見ているエラーを修正します。resources :registrationsPortal::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 に答える