Rails 路由的未初始化常量问题

Posted

技术标签:

【中文标题】Rails 路由的未初始化常量问题【英文标题】:Uninitialized constant problem for Rails routes 【发布时间】:2010-11-10 17:26:42 【问题描述】:

这是我的路线配置:

map.resources :services 做 |services|
  services.resources :capabilities 做 |capabilities|
    能力.资源:http_headers
  结尾
结束

这是我的“rake routes”输出:

laran:trunk laran$ rake 路由
(在 /Users/laran/workspace/kibo/mega/server/trunk)
                           帐户 GET /accounts(.:format) :action=>"index", :controller=>"accounts"
                                    POST /accounts(.:format) :action=>"create", :controller=>"accounts"
                        new_account GET /accounts/new(.:format) :action=>"new", :controller=>"accounts"
                       edit_account GET /accounts/:id/edit(.:format) :action=>"edit", :controller=>"accounts"
                            account GET /accounts/:id(.:format) :action=>"show", :controller=>"accounts"
                                    PUT /accounts/:id(.:format) :action=>"update", :controller=>"accounts"
                                    删除 /accounts/:id(.:format) :action=>"destroy", :controller=>"accounts"
                           服务 GET /services(.:format) :action=>"index", :controller=>"services"
                                    POST /services(.:format) :action=>"create", :controller=>"services"
                        new_service GET /services/new(.:format) :action=>"new", :controller=>"services"
                       edit_service GET /services/:id/edit(.:format) :action=>"edit", :controller=>"services"
                            服务 GET /services/:id(.:format) :action=>"show", :controller=>"services"
                                    PUT /services/:id(.:format) :action=>"update", :controller=>"services"
                                    删除 /services/:id(.:format) :action=>"destroy", :controller=>"services"
               service_capabilities GET /services/:service_id/capabilities(.:format) :action=>"index", :controller=>"capabilities"
                                    POST /services/:service_id/capabilities(.:format) :action=>"create", :controller=>"capabilities"
             new_service_capability GET /services/:service_id/capabilities/new(.:format) :action=>"new", :controller=>"capabilities"
            edit_service_capability GET /services/:service_id/capabilities/:id/edit(.:format) :action=>"edit", :controller=>"capabilities"
                 service_capability GET /services/:service_id/capabilities/:id(.:format) :action=>"show", :controller=>"capabilities"
                                    PUT /services/:service_id/capabilities/:id(.:format) :action=>"update", :controller=>"capabilities"
                                    删除 /services/:service_id/capabilities/:id(.:format) :action=>"destroy", :controller=>"capabilities"
    service_capability_http_headers GET /services/:service_id/capabilities/:capability_id/http_headers(.:format) :action=>"index", :controller=>"http_headers"
                                    POST /services/:service_id/capabilities/:capability_id/http_headers(.:format) :action=>"create", :controller=>"http_headers"
 new_service_capability_http_header GET /services/:service_id/capabilities/:capability_id/http_headers/new(.:format) :action=>"new", :controller=>"http_headers"
edit_service_capability_http_header GET /services/:service_id/capabilities/:capability_id/http_headers/:id/edit(.:format) :action=>"edit", :controller=>"http_headers"
     service_capability_http_header GET /services/:service_id/capabilities/:capability_id/http_headers/:id(.:format) :action=>"show", :controller=>"http_headers"
                                    PUT /services/:service_id/capabilities/:capability_id/http_headers/:id(.:format) :action=>"update", :controller=>"http_headers"
                                    删除 /services/:service_id/capabilities/:capability_id/http_headers/:id(.:format) :action=>"destroy", :controller=>"http_headers"
                                           /login :action=>"login", :controller=>"accounts"
                                           /logout :action=>"注销", :controller=>"accounts"
                               根 / :action=>"index", :controller=>"default"
laran:trunk laran$ 

当我转到 /services/new 时,我收到此错误:

ServicesController 中的名称错误#new

未初始化的常量 ServicesController::Services

什么给了?我怎样才能让事情正常工作和路由?谢谢。

【问题讨论】:

我想通了。这样的笨蛋。我将控制器创建为 ServicesController,因此所有引用都是对服务模型而不是服务模型的引用。愚蠢的错误。 【参考方案1】:

ServicesController 是否由模型 Service 支持?您是否不小心在控制器中将其引用为服务?

【讨论】:

这正是我所做的。感谢您的回复。 确保控制器文件的名称是 services_controller.rb(复数)【参考方案2】:

这可能与您的路线无关;您在 ServicesController 中的 new 方法正在尝试使用名为 Services 的(类?常量?对象?),但它不存在。

【讨论】:

以上是关于Rails 路由的未初始化常量问题的主要内容,如果未能解决你的问题,请参考以下文章

HomePresenter 的未初始化常量

使用 ActiveMerchant::Billing::EwayManagedGateway 的未初始化常量错误

Rails:NameError:未初始化的常量

rails 链接表“未初始化的常量”错误

Rails:NameError(未初始化的常量 UserSerializer)

Heroku + Rails + GeoIp = 未初始化的常量 GeoIP 错误