问题描述
我已经使用
rails generate scaffold myOdb2 columna:integer, columnB:string
现在我想添加columnc:string
.我应该怎么办?
now I want to add columnc:string
. What should I do?
顺便说一句:问题很普遍,但可以通过 Rubymine 更快地完成吗?
BTW: question is general but can it be done quicker through Rubymine?
推荐答案
如果你刚刚生成它并意识到你的错误,你可以使用:
If you've just generated it and realized your mistake you can use:
rails 破坏脚手架 myOdb2
然后重新生成脚手架:
rails 生成脚手架 myOdb2 columna:integer, columnB:string, columnc:string
如果您对要保留的脚手架创建的模型进行了一些更改,但不介意破坏控制器和视图:
If you've made some changes to the scaffold-created model that you want to keep but you don't mind destroying the controller and views:
rails 销毁 scaffold_controller myOdb2
然后创建迁移以添加列:
then create a migration to add the column:
rails 生成迁移 add_columnc_to_myodb2s columnc:string
然后重新生成控制器和视图:
then re-generate the controller and views:
rails 生成 scaffold_controller myOdb2 columna:integer, columnB:string, columnc:string
如果您对控制器或视图进行了更改,您只需运行迁移以更新数据库和模型,然后手动将新列添加到您的每个视图中.
If you've made changes to the controller or views, you'll need to just run the migration to update the database and model, then manually add the new column to each of your views.
这篇关于向 Rails 模型添加新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!