Hamza1510
Hamza1510

Reputation: 47

Uninitialized constant Post in rails console, following tutorial

So I'm following this tutorial, and I have triple checked that everything I've done up to this point is correct. Tutorial: https://www.youtube.com/watch?v=0OzDgi0zqJU

At the beginning of the video, he wants me to start the rails console and type "Post.all". I'm getting this error (below) while his terminal displaying a empty array.

EIERs-MBP:training eier$ rails c
Running via Spring preloader in process 75415
Loading development environment (Rails 5.0.1)
2.3.0 :001 > Post.all
NameError: uninitialized constant Post
from (irb):1
from /Users/eier/.rvm/gems/ruby-2.3.0@global/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/eier/.rvm/gems/ruby-2.3.0@global/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/eier/.rvm/gems/ruby-2.3.0@global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/eier/.rvm/gems/ruby-2.3.0@global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/eier/.rvm/gems/ruby-2.3.0@global/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from /Users/eier/Documents/Workspace/rails/training/bin/rails:9:in `<top (required)>'
from /Users/eier/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/eier/.rvm/rubies/ruby-2.3.0/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'

I've looked everywhere for answers on what this might be, I don't know if it's because of the rails version I'm using or not. It's a really simple tutorial, so it's really frustrating. We're suppose to create content in the rails console with Post.create(title: "first post", content: "test"), but I'm stuck on the error when typing "Post.all".

Here's the posts_controller.rb file:

class PostsController < ApplicationController
 def index

 end
end

This is what his file looks as well, and all other files such as index.html.erb and routes we've created. Please let me know if I need to provide more information.

Upvotes: 2

Views: 1940

Answers (2)

Sravan
Sravan

Reputation: 18647

First create a model Post using the command,

rails generate model Post title:string text:text

This creates app/models/post.rb and a migratiuon file in db/migrate

Now, run the migration, if you already have the database.

rails db:migrate

Now, the table is created.

Now open rails console/rails c

Post.all

Upvotes: 1

Glyoko
Glyoko

Reputation: 2090

Your posts_controller.rb has nothing to do with it. Check that your app/models/post.rb defines

class Post < ApplicationRecord # sometimes "< ActiveRecord::Base" instead
end

When you type Post.all, ruby tries to lookup a class called Post–your model–and call the all method on it. If it says uninitialized constant Post in the error, then your post model must not be defined.

Upvotes: 0

Related Questions