NameError: uninitialized constant className - ruby-on-rails

I am getting error in irb
NameError: uninitialized constant Student
for Student.new or whatever model operations are there.
But in rails it gives no error and it works fine. What would be the reason?
This error only happens in Windows, same code I have in Linux and there it works fine.
What makes the difference here?

irb has nothing to do with your rails project.
What you want instead is to run
rails console
from within your rails project directory. Here you have access to everything defined within the application - Rails loads everything automatically.

rails c
Create new tab in terminal. Here you may work with modals. Like CRUD operations.
But make sure your terminal must pointing in rails working directory before trying rails c command ...

Related

Rails server broken after Action Cable and using rails restart command

Going through the following book with the error showing: Agile Web Development with Rails 5.1, iteration F5: Broadcasting with Action Cable. As far as I am concerned I followed the directions precisely, my only possible flaw was to type: rails restart rather than rails server again, now it will just not boot anymore! Any possible help is appreciated!
Error in Question:
/Users/dj911ice/.rvm/gems/ruby-2.4.1/gems/railties-5.1.2/lib/rails/railtie/configuration.rb:95:in method_missing: undefined method action_cable_ for # (NoMethodError)
The server error:
Looks like you have a typo around line 57 of config/environments/development.rb, where you are calling config.action_cable_ instead of config.action_cable.
Remove the trailing underscore and restart your rails server.

How do I get classes autoloaded in the rails console?

There are obviously some classes that are automatically loaded when I start "irb" using "rails c". So how do I ensure my own classes are loaded? Am I expected to use "require"?
I get "NameError: uninitialized constant" whenever I try to use my classes.
If the classes are included as part of the Rails app though the Gem bundle, or you're require-ing the classes elsewhere in the app, then they should be loaded along with the Rails app into the console.
If they're completely indepent of Rails (e.g. the Rails app doesn't load these classes), then you'll have to require them explicitly.

Rubymine using generator from rails console

I am currently using Rubymine for ruby on rails development.
After enjoying the console for testing purposes, I want to use the console instead of the build-in Rails Generator for creating new models and such.
But I can't figure out how to use the rails console (Tools | Run Rails Console) for generating -lets say- a new model "user".
rails generate model user name:string
gives me
NameError: undefined local variable or method `string' for main:Object
I think I'm just missing something very basic here but I couldn't come up with a solution by myself after several tries :/
I hope you can help me out.
Greets
If you feel that you really must use the console to do this, you have a few options to get to the system.
You can use backticks.
or
You can use system()
Your command would then look like system("rails generate model user name:string") or it would look like `rails generate model user name:string`
The command you are trying to run is not something that should be run from a ruby console. It is a bash command. The error you are getting is because you are trying to run something that is not ruby in a ruby environment.

NameError: uninitialized constant Object::User

I searched the other posts that had this issue and could not find one that fixed my particular issue. My irb is going crazy. I am trying to change user roles in my database but I can't even get to my users!
irb(main):001:0> User.all
NameError: uninitialized constant Object::User
from (irb):1
from c:/Ruby192/bin/irb:12:in `<main>'
It was working fine, stopped working, was fine, and now stopped again. I have a User model and users added. I cannot pinpoint the issue. Let me know what code you need to see. Thanks!
You should run
ruby script/console # Rails < 3
or
rails c # Rails 3.x
But not (I believe you've just run this)
irb
Make sure you're actually using the rails console command rather than just running irb in your project folder.

Rails 2.3.9, adding ActiveScaffold, getting in `alias_method': undefined method `number_of_pages' for class `Paginator'

I have AS (as a plugin) working in another app just fine (I dont remember this
issue...) - its also using Rails 2.3.9, on same dev box.
Now trying to add it to a new app and getting this error when running
the webrick, script/server:
/Users/kimptoc/.rvm/gems/ruby-1.8.7-p302/gems/activesupport-2.3.9/lib/
active_support/core_ext/module/aliasing.rb:33:in `alias_method':
undefined method `number_of_pages' for class `Paginator' (NameError)
from /Users/kimptoc/.rvm/gems/ruby-1.8.7-p302/gems/
activesupport-2.3.9/lib/active_support/core_ext/module/aliasing.rb:
33:in `alias_method_chain'
from /Users/kimptoc/Documents/ruby/borisbikestats/vendor/plugins/
active_scaffold/lib/extensions/paginator_extensions.rb:9
I couldn't find any previous references to this error.
Embarassingly I dont know which version of AS I am using successfully... it says 1.2RC1 in the CHANGELOG file, but I seem to remember trying a few of the forks...(is there somewhere I can see which fork is in use...). I have tried using the the 'main' activescaffold in the new project, but it fails as above, but so does using the same plugin that is in the working project :(
Thanks in advance for any pointers as to what I am doing wrong.
Thanks,
Chris
PS Just realised that new Rails was using Rails 3 and old/working one was 2.3.9. It seems you need to use an alternate branch of AS for Rails 3, like this : https://github.com/vhochstein/active_scaffold/wiki
This is just a guess, but built in pagination was removed in Rails 2 as I recall, and you needed to install the classic_pagination plugin to get that functionality back.
I only used ActiveScaffold once, but the error there says paginator_extensions.rb ... it's probably monkey patching the old Rails paginator? Therefore the class exists but the number_of_pages method doesn't.

Resources