我收到此错误:
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文件。 Radar提到了我发布的要点,但我想我会把代码放在这里以防gist被删除。
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
答案 0 :(得分:10)
为了说清楚:marcamillion进入了#rubyonrails和Freenode以及shared his config/routes.rb
。这就是我弄清楚他正在制作的错误然后找到答案的地方。
两件事。
而不是像这样定义admin
命名空间的根路由:
match 'admin' => 'admin/dashboard#index', :as => :admin_root
在namespace :admin
调用中定义它,如下所示:
namespace 'admin' do
root :to => "dashboard#index"
end
这会自动将其定义为admin_root_path
和admin_root_url
,因此您无需这样做,它会自动为控制器添加admin/
前缀,因此您赢了也不需要这样做。
目前您的config/routes.rb
:
resources :registrants, :controller => 'portal/registrants' do
resources :registrations
end
这将使用registrants
正确定义Portal::RegistrantsController
的资源路由,但将使用Portal::RegistrationsController
定义下面的嵌套路由。 将做的是尝试使用RegistrationsController
,这就是您收到该错误的原因。
要解决此问题,我建议您使用scope
方法,如下所示:
scope :module => Portal do
resources :registrants do
resources :registrations
end
end
scope
方法以这种方式使用时,会告诉Rails块内路由的控制器位于Portal
命名空间下。这意味着它会知道resources :registrants
适用于Portal::Registrants
而resources :registrations
适用于Portal::Registrations
,从而可以解决您所看到的错误。
答案 1 :(得分:0)
如果您更改此行,该怎么办:
class Portal::RegistrationsController < ApplicationController
为:
class RegistrationsController < ApplicationController
答案 2 :(得分: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
注册人:: ApplicationController(/app/controllers/registrants/application_controller.rb
)控制器的目的是避免代码重复。