我的数据库没有保存

My database doesn't save

我正在为初学者做铁路。我创建模型 Post,标题为字符串,body 为文本区域。在那之后,我忘记了以 sub body

的形式添加新元素

所以,我在 20150120154140_create_posts.rb 和 schema.rb 中添加子 body,如下所示

class CreatePosts < ActiveRecord::Migration
def change
 create_table :posts do |t|
   t.string :title   
   #Add code below
   t.string :subbody
   t.text :body
   t.timestamps
  end
 end
end

这是我的schema.rb

ActiveRecord::Schema.define(version: 20150122040119) do

 create_table "posts", force: true do |t|
  t.string   "title"
  t.string   "subbody"
  t.text     "body"
  t.datetime "created_at"
  t.datetime "updated_at"
  end

end

添加后,我在终端上写 rake db:reset。

当我检查 IRB 中的数据库时,它显示在 table。

在我修改视图以创建新的 Post 并提交后,它没有保存。

在 IRB 中检查,它在 subbody

处表示为 nil

rake db:reset 不会 运行 迁移。您需要 rake db:migrate:down,然后是 rake db:migrate。只要这是您的最新迁移,那应该就可以工作。有关详细信息,请参阅 this post