# Uninitialized constant error

**URL:** <https://www.ruby-forum.com/t/uninitialized-constant-error/52349>\
**Category:** Rails\
**Created:** [January 13, 2006, 12:39am UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349 "2006-01-13T00:39:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 13, 2006, 12:39am UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/1 "2006-01-13T00:39:11Z")

</div>

Hi Everyone,

I’m a newbie to Rails, and I just finished going through the cookbook  
tutorial. I’m now trying to build my own application, and I’m trying to  
add more inputs to the edit view to modify data in other tables. Below  
is the controller code where I am having trouble. PartsLocation is  
another table (like Part) in the database, and that is the line where I  
get the error.

class PartController \< ApplicationController  
scaffold :part

```
def list
	@parts = Part.find_all
end

def edit
	@part = Part.find(@params["id"])
	@partslocation = PartsLocation.find(@params["partnumber"])
end

```

end

Below is the error I am seeing related to the PartsLocation table, but I  
have no idea why PartsLocation is uninitialized, but Part is  
initialized. Any help is appreciated!

Thanks,  
Paul

NameError in Part#edit

uninitialized constant PartsLocation

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:200:in  
`const_missing' #{RAILS_ROOT}/app/controllers/part_controller.rb:10:in`edit’

C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:200:in  
`const_missing' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:853:in`perform\_action\_without\_filters’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/filters.rb:332:in  
`perform_action_without_benchmark' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in`perform\_action\_without\_rescue’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/benchmarking.rb:69:in`perform\_action\_without\_rescue’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/rescue.rb:82:in  
`perform_action' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/base.rb:369:in`process\_without\_session\_management\_support’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/session\_management.rb:116:in  
`process' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in`dispatch’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick\_server.rb:117:in  
`handle_dispatch' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in`service’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in  
`service' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in`run’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:173:in  
`start_thread' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:162:in`start\_thread’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:92:in`start’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:82:in`start’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick\_server.rb:69:in  
`dispatch' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in`require’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:214:in  
`require' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in`require’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:214:in  
`require’  
script/server:3

C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active\_support/dependencies.rb:200:in  
`const_missing' #{RAILS_ROOT}/app/controllers/part_controller.rb:10:in`edit’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/base.rb:853:in  
`perform_action_without_filters' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/filters.rb:332:in`perform\_action\_without\_benchmark’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/benchmark.rb:293:in`measure’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/benchmarking.rb:69:in  
`perform_action_without_rescue' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/rescue.rb:82:in`perform\_action’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action\_controller/base.rb:369:in  
`process_without_session_management_support' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/actionpack-1.11.2/lib/action_controller/session_management.rb:116:in`process’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:38:in  
`dispatch' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in`handle\_dispatch’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick\_server.rb:83:in  
`service' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in  
`run' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:173:in`start\_thread’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:162:in  
`start_thread' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:95:in`start’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:23:in`start’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in`dispatch’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in`require’  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28  
C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:21:in  
`require' C:/RUBYON~1/INSTAN~1.0/ruby/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in`require’  
script/server:3

This error occured while loading the following files:  
parts\_location.rb

Request

Parameters: {“id”=\>“1”}

Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 13, 2006, 12:56am UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/2 "2006-01-13T00:56:17Z")

</div>

> class PartController \< ApplicationController  
> scaffold :part
> 
> def list  
> @parts = Part.find\_all  
> end
> 
> def edit  
> @part = Part.find(@params[“id”])  
> @partslocation = PartsLocation.find(@params[“partnumber”])  
> end  
> end

my guess is you are looking for something like this…

class PartController \< ApplicationController  
scaffold :part

def list…

def edit  
@part = Part.find(params[“id”])  
@partslocation =  
PartLocations.find\_by\_partnumber(params[“partnumber”)  
end  
end

The error you are getting is because it cannot find the ‘parts\_location’  
model. Probably you either have it defined in ‘part\_locations’ or  
‘partslocation’.

Your second search will fail unless “partnumber” is the primary key for  
that table.

\_Kevin

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 13, 2006, 3:09am UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/3 "2006-01-13T03:09:50Z")

</div>

On Jan 12, 2006, at 4:39 PM, Paul H. wrote:

> where I  
> @part = Part.find(@params[“id”])  
> @partslocation = PartsLocation.find(@params[“partnumber”])  
> end  
> end

Scaffolding is great, but if you are going to step beyond the bounds  
of the basics, then you have to get in there and get your hands  
dirty. The error is caused by the lack of:

model :parts\_location

But if you are using 2 models, you need to specify them both, like

model :part, :parts\_location

In either case, you’ll need to ditch the scaffold, and do it for real  
now.

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 14, 2006, 5:53pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/4 "2006-01-14T17:53:26Z")

</div>

I tried both Kevin and Lori’s suggestions, but neither seemed to help at  
all. I think part of my problem is that it is looking for a  
parts\_location.rb file, and my model file is partslocation.rb. Why is  
it putting the underscore in there?

As far as ditching the scaffolding goes, I was hoping to replace it a  
bit at a time by removing list, then edit, etc. I know it will  
eventually be gone.

Thanks,  
Paul

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 14, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/5 "2006-01-14T17:59:24Z")

</div>

On 1/14/06, Paul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I tried both Kevin and Lori’s suggestions, but neither seemed to help at  
> all. I think part of my problem is that it is looking for a  
> parts\_location.rb file, and my model file is partslocation.rb. Why is  
> it putting the underscore in there?
> 
> As far as ditching the scaffolding goes, I was hoping to replace it a  
> bit at a time by removing list, then edit, etc. I know it will  
> eventually be gone.

Your model should be part\_location.rb. Didn’t you create your model via  
$ script/generate model PartLocation?

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 14, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/6 "2006-01-14T18:07:40Z")

</div>

Joe Van D. wrote:

> On 1/14/06, Paul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I tried both Kevin and Lori’s suggestions, but neither seemed to help at  
> > all. I think part of my problem is that it is looking for a  
> > parts\_location.rb file, and my model file is partslocation.rb. Why is  
> > it putting the underscore in there?
> > 
> > As far as ditching the scaffolding goes, I was hoping to replace it a  
> > bit at a time by removing list, then edit, etc. I know it will  
> > eventually be gone.
> 
> Your model should be part\_location.rb. Didn’t you create your model via  
> $ script/generate model PartLocation?

I believe I did:

ruby script\generate model partslocation

Paul

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 14, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/7 "2006-01-14T19:00:39Z")

</div>

On 1/14/06, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > eventually be gone.
> > > 
> > > Your model should be part\_location.rb. Didn’t you create your model via  
> > > $ script/generate model PartLocation?
> > 
> > I believe I did:
> > 
> > ruby script\generate model partslocation
> 
> Should’ve used script/generate model parts\_location (or  
> PartsLocation). That’s what the controller expects to find.

In any event, I bet your model is called Partslocation, right? Your  
controller is looking for PartsLocation.

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 14, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/8 "2006-01-14T18:57:38Z")

</div>

On 1/14/06, Paul H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Your model should be part\_location.rb. Didn’t you create your model via  
> > $ script/generate model PartLocation?
> 
> I believe I did:
> 
> ruby script\generate model partslocation

Should’ve used script/generate model parts\_location (or  
PartsLocation). That’s what the controller expects to find.

---

<div class="post-metadata">

**Author:** ![phudson1442](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c68b51/32.png) [@phudson1442](https://www.ruby-forum.com/u/phudson1442)\
**Post date:** [January 14, 2006, 8:06pm UTC](https://www.ruby-forum.com/t/uninitialized-constant-error/52349/9 "2006-01-14T20:06:59Z")

</div>

Joe Van D. wrote:

> On 1/14/06, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > > eventually be gone.
> > > > 
> > > > Your model should be part\_location.rb. Didn’t you create your model via  
> > > > $ script/generate model PartLocation?
> > > 
> > > I believe I did:
> > > 
> > > ruby script\generate model partslocation
> > 
> > Should’ve used script/generate model parts\_location (or  
> > PartsLocation). That’s what the controller expects to find.
> 
> In any event, I bet your model is called Partslocation, right? Your  
> controller is looking for PartsLocation.

Now I feel like an idiot. All that I needed was a lowercase L in  
Partslocation. It worked just fine after I changed it. Nevertheless,  
thanks for the help everyone.

Thanks,  
Paul
