mar*_*ion 2 ruby-on-rails ruby-on-rails-3
我收到此错误:
Routing Error
uninitialized constant RegistrationsController
Run Code Online (Sandbox Code Playgroud)
这是我的日志:
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)
Run Code Online (Sandbox Code Playgroud)
这是我的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"}
Run Code Online (Sandbox Code Playgroud)
这是我的注册控制器,位于/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
Run Code Online (Sandbox Code Playgroud)
当我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
Run Code Online (Sandbox Code Playgroud)
Rya*_*igg 10
为了说清楚:marcamillion进入#rubyonrails和Freenode并分享他的config/routes.rb
.这就是我弄清楚他正在制造的错误然后想出答案的地方.
两件事情.
而不是admin
像这样定义命名空间的根路由:
match 'admin' => 'admin/dashboard#index', :as => :admin_root
Run Code Online (Sandbox Code Playgroud)
在namespace :admin
调用中定义它,如下所示:
namespace 'admin' do
root :to => "dashboard#index"
end
Run Code Online (Sandbox Code Playgroud)
这将自动将其定义为admin_root_path
和admin_root_url
,因此您不需要这样做,它将自动为控制器添加admin/
前缀前缀,因此您也不需要这样做.
你现在有这个config/routes.rb
:
resources :registrants, :controller => 'portal/registrants' do
resources :registrations
end
Run Code Online (Sandbox Code Playgroud)
这将做的是定义资源路由以registrants
正确使用Portal::RegistrantsController
,但不会在此下面定义嵌套路由Portal::RegistrationsController
.它会做什么尝试使用RegistrationsController
,这就是你得到这个错误的原因.
要解决这个问题,我建议你使用这个scope
方法,如下所示:
scope :module => Portal do
resources :registrants do
resources :registrations
end
end
Run Code Online (Sandbox Code Playgroud)
该scope
方法,以这种方式使用的时候,会告诉Rails,对于块内的路由控制器下Portal
的命名空间.这意味着它会知道resources :registrants
是Portal::Registrants
那resources :registrations
是Portal::Registrations
,由此固定,你看到的错误.
归档时间: |
|
查看次数: |
3878 次 |
最近记录: |