未完成的常量Post在rails控制台中,遵循教程

时间:2017-01-18 06:05:14

标签: ruby-on-rails ruby rails-console

所以我正在学习本教程,并且我已经三次检查了我到目前为止所做的一切是否正确。教程:https://www.youtube.com/watch?v=0OzDgi0zqJU

在视频开头,他希望我启动rails控制台并输入“Post.all”。当他的终端显示一个空数组时,我收到此错误(下面)。

class EventTableViewController: UITableViewController {

private let tableData:[Cell] = [
    Cell(
        image: "burj_k",
        title: "Hola",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 1",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 2",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 3",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 4",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 5",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 6",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 7",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 8",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 9",
        place: "DFX",
        date: "SDSDs"
    ),
    Cell(
        image: "burj_k",
        title: "Hola 10",
        place: "DFX",
        date: "SDSDs"
    )
]



override func viewDidLoad() {
    super.viewDidLoad()
    //remembersLastFocusedIndexPath = true

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
}

我到处寻找可能是什么的答案,我不知道是不是因为我正在使用的rails版本。这是一个非常简单的教程,所以非常令人沮丧。我们假设使用Post.create在rails控制台中创建内容(标题:“first post”,内容:“test”),但在输入“Post.all”时我会遇到错误。

这是posts_controller.rb文件:

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>'

这也是他的文件以及所有其他文件,例如index.html.erb和我们创建的路由。如果我需要提供更多信息,请告诉我。

2 个答案:

答案 0 :(得分:1)

首先使用命令

创建模型Post

rails generate model Post title:string text:text

这会在app/models/post.rb

中创建db/migrate和migratiuon文件

现在,如果您已有数据库,请运行迁移。

rails db:migrate

现在,该表已创建。

现在打开rails console / rails c

Post.all

答案 1 :(得分:0)

你的posts_controller.rb与它无关。检查您的app / models / post.rb是否定义了

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

当您键入Post.all时,ruby会尝试查找名为Post的类 - 您的模型 - 并在其上调用all方法。如果错误中显示uninitialized constant Post,则不得定义您的帖子模型。