Adding a new field to Rails model

前端 未结 4 1920
臣服心动
臣服心动 2021-01-30 11:03

I already created a scafold using

rails generate scaffold myOdb2 columna:integer, columnB:string

now I want to add columnc:string

4条回答
  •  失恋的感觉
    2021-01-30 11:57

    • If you've just generated it and realized your mistake you can use:

      rails destroy scaffold myOdb2

      and then re-generate the scaffolding:

      rails generate scaffold 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 destroy scaffold_controller myOdb2

      then create a migration to add the column:

      rails generate migration add_columnc_to_myodb2s columnc:string

      then re-generate the controller and views:

      rails generate 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.

提交回复
热议问题