Forum: Ruby on Rails uninitialized constant error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul H. (Guest)
on 2006-01-13 01:39
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"}
Kevin O. (Guest)
on 2006-01-13 01:56
>
> 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
Lori O. (Guest)
on 2006-01-13 04:09
(Received via mailing list)
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.
Paul H. (Guest)
on 2006-01-14 18:53
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
Joe Van D. (Guest)
on 2006-01-14 18:59
(Received via mailing list)
On 1/14/06, Paul H. <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?
Paul H. (Guest)
on 2006-01-14 19:07
Joe Van D. wrote:
> On 1/14/06, Paul H. <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
Joe Van D. (Guest)
on 2006-01-14 19:57
(Received via mailing list)
On 1/14/06, Paul H. <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.
Joe Van D. (Guest)
on 2006-01-14 20:00
(Received via mailing list)
On 1/14/06, Joe Van D. <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.
Paul H. (Guest)
on 2006-01-14 21:06
Joe Van D. wrote:
> On 1/14/06, Joe Van D. <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
This topic is locked and can not be replied to.