Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails NameError uninitialized constant (Model and Namespace Collision)

I have a model named Organization. It is defined in app/models/organization.rb

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

I have a controller named Admin::Organization::ActivitiesController. It is defined in 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

@organization = ::Organization.new

then it works fine.

This behavior doesn't seem to appear in a pry console. 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.

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. There is a namespace that uses the organization word.

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 ::. 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?

like image 973
CodeSmith Avatar asked Oct 12 '15 20:10

CodeSmith


People also ask

What is uninitialized constant class error in rails?

rails nameerror uninitialized constant class will occur if your rails console is not loaded with configuration of the class file containing method being called. Basically this can happen if you happened to call the function from the class which is not loaded with configuration when your rails console/server was started.

Can I use a class as a namespace in rails?

If you use a class as a namespace, it can produce a bug that doesn’t always show up on the surface. You should different names for your model class and your namespace in Rails applications.

What is an uninitialized constant error in Ruby?

Ruby NameError Uninitialized Constant Causes The Uninitialized Constant error is a variation of a regular NameError exception class. It has several possible causes. You'll see this error when the code refers to a class or module that it can't find, often because the code doesn't include require, which instructs the Ruby file to load the class.

What is the nameerror uninitialized constant error?

One of the most vexing is the NameError Uninitialized Constant exception because it has more than one cause. The syntax of the exception follows this format: The Uninitialized Constant error is a variation of a regular NameError exception class. It has several possible causes.


1 Answers

If you just want to make the path correct, you don't require to put the activities controller under admin/organization namespace folder. 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
......
like image 195
Ashik Salman Avatar answered Nov 14 '22 23:11

Ashik Salman