简体   繁体   English

Rails NameError未初始化的常量(模型和命名空间碰撞)

[英]Rails NameError uninitialized constant (Model and Namespace Collision)

I have a model named Organization . 我有一个名为Organization的模型。 It is defined in app/models/organization.rb 它在app/models/organization.rb定义

class Organization < ActiveRecord::Base
... code
end

I have a controller named Admin::Organization::ActivitiesController . 我有一个名为Admin::Organization::ActivitiesController的控制器。 It is defined in app/controllers/admin/organization/activities_controller.rb . 它在app/controllers/admin/organization/activities_controller.rb It has an index action in it. 它有一个索引动作。

class Admin::Organization::ActivitiesController < ApplicationController
  def index
    @organization = Organization.new
    ... more code
  end
end

I get the following message when I execute the above index action: 执行上面的索引操作时,我收到以下消息:

NameError in Admin::Organization::ActivitiesController#index
uninitialized constant Admin::Organization::ActivitiesController::Organization

For some reason it's scoping the Organization model inside the controller class. 由于某种原因,它在控制器类中确定了组织模型。 If I change the index method to use 如果我改变index方法使用

@organization = ::Organization.new

then it works fine. 然后它工作正常。

This behavior doesn't seem to appear in a pry console. 此行为似乎没有出现在pry控制台中。 If I add a binding.pry call in the index method, then I can call Organization.new or ::Organization.new from the command line it works fine. 如果我在index方法中添加一个binding.pry调用,那么我可以从命令行调用Organization.new::Organization.new ,它可以正常工作。

Every other model in the app works correctly and doesn't have this weird behavior. 应用程序中的每个其他模型都能正常工作,并且没有这种奇怪的行为。 I didn't write the code originally so I'm trying to figure out what is going on. 我最初没有写代码,所以我想弄清楚发生了什么。

I think it might have something do with a namespace in the routes.rb file. 我认为它可能与routes.rb文件中的命名空间有关。 There is a namespace that uses the organization word. 有一个名称空间使用organization单词。

namespace :admin do
  namespace :organization
    resources :activities
  end
end

As a test, I changed the namespace to :organizations , and I was able to get things to work without needing :: . 作为测试,我将名称空间更改为:organizations ,并且我能够在不需要:: Is there a way to structure things, or a routing setting, so we can have a namespace of :organization that doesn't interfere with the model named Organization ? 有没有办法构建事物或路由设置,所以我们可以有一个命名空间:organization不干扰名为Organization的模型?

If you just want to make the path correct, you don't require to put the activities controller under admin/organization namespace folder. 如果您只想使路径正确,则不需要将活动控制器放在admin/organization命名空间文件夹下。 Another option would be like using scope instead of namespace. 另一个选择是使用范围而不是命名空间。

# app/controllers/activities_controller.rb
class ActivitiesController < ApplicationController
  def index
    @organization = Organization.new
    ... more code
  end
end

Now configure routes, 现在配置路由,

# config/routes.rb
scope 'admin/organization', path: 'admin/organization'  do
  resources :activities
end

This will produce routes like this, 这将产生这样的路线,

Prefix Verb   URI    Pattern                                      Controller#Action

activities    GET    /admin/organization/activities(.:format)     activities#index
              POST   /admin/organization/activities(.:format)     activities#create
......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM