packat
2008-Nov-28 13:49 UTC
Beginner: "NameError - uninitialized constant" when I create a model object
My first Rails example using model and I got this NameError: NameError in VandaController#att uninitialized constant VandaController::Compute Same code works fine if I don''t create the Compute object. What did I do wrong? Here is the Controller class VandaController < ApplicationController def att @compute = Compute.new @data1 = @compute.cmpt end end Here is the Model class Compute def cmpt return 5 end end And here is the view <html> <head> <title>Model</title> </head> <body> <h1>reading data from a model</h1> <br> <br> Fetch : <%= @data1 %> from Compute model. <br> </body> </html> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-28 14:28 UTC
Re: Beginner: "NameError - uninitialized constant" when I create a model object
On Nov 28, 1:49 pm, packat <cpete...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My first Rails example using model and I got this NameError: > NameError in VandaController#att > uninitialized constant VandaController::Compute >Is the model in compute.rb in app/models or lib ? Fred> Same code works fine if I don''t create the Compute object. > What did I do wrong? > > Here is the Controller > class VandaController < ApplicationController > def att > @compute = Compute.new > @data1 = @compute.cmpt > end > end > > Here is the Model > class Compute > def cmpt > return 5 > end > end > > And here is the view > <html> > <head> > <title>Model</title> > </head> > <body> > <h1>reading data from a model</h1> > <br> > <br> > Fetch : <%= @data1 %> from Compute model. > <br> > </body> > </html>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
packat
2008-Nov-28 14:57 UTC
Re: Beginner: "NameError - uninitialized constant" when I create a model object
It is in app/models On Nov 28, 9:28 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 28, 1:49 pm, packat <cpete...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My first Rails example using model and I got this NameError: > > NameError in VandaController#att > > uninitialized constant VandaController::Compute > > Is the model in compute.rb in app/models or lib ? >It is in app/models. I am following a book "Beginning Ruby on Rails" by Steven Holzner. That book may be based on older version. Mine is 1.8.6 After posting the question, I blindly tried running "ruby script/generate model compute" and now get a different error: =============== ActiveRecord::StatementInvalid in VandaController#att Mysql::Error: Table ''orchids.computes'' doesn''t exist: SHOW FIELDS FROM `computes` ===============Apparently, a schema is created for Compute and Ruby thought ther is a Compute table in the database. So this is not helping any.> FredThanks Fred --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Nov-28 15:31 UTC
Re: Beginner: "NameError - uninitialized constant" when I create a model object
On Nov 28, 2:57 pm, packat <cpete...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> It is in app/models > > On Nov 28, 9:28 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Nov 28, 1:49 pm, packat <cpete...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My first Rails example using model and I got this NameError: > > > NameError in VandaController#att > > > uninitialized constant VandaController::Compute > > > Is the model in compute.rb in app/models or lib ? > > It is in app/models. >The important thing was whether the file was called compute.rb> I am following a book "Beginning Ruby on Rails" by Steven Holzner. > That book may be based on older version. Mine is 1.8.6The important thing would be the rails version, not the ruby version.> > and now get a different error: > ===============> ActiveRecord::StatementInvalid in VandaController#att > Mysql::Error: Table ''orchids.computes'' doesn''t exist: SHOW FIELDS FROM > `computes` > ===============> Apparently, a schema is created for Compute and Ruby thought ther is a > Compute table in the database. > So this is not helping any.It would have created a migration to add that table but it would not have run it. You can run it with rake db:migrate Fred> > > Fred > > Thanks Fred--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---