如何在运行rails生成模型/脚手架之后添加字段?

时间:2022-11-24 09:22:36

Let's say I forgot to add a field to my model. How do I go about adding it cleanly? Do I need to re-run rails generate or can I edit a file somewhere?

假设我忘记给模型添加一个字段。我怎么才能把它加得更干净呢?我需要重新运行rails生成还是可以在某处编辑文件?

2 个解决方案

#1


18  

Either you should re-run your generate scaffold/model (will blow away existing code)

您应该重新运行生成的scaffold/model(这会破坏现有的代码)

or you can add the field directly to the database and your view

或者可以直接将字段添加到数据库和视图中

Rails3: rails generate migration add_column_name_to_table_name column_name:string

Rails3: rails生成迁移add_column_name_to_table_name column_name:string

or

Rails2.x: ruby script/generate migration addColumnToTableName column_name:string

Rails2。x: ruby脚本/生成迁移的addColumnToTableName column_name:string

which will generate a file in db/migrate which you can apply with a 'rake db:migrate'

它将在db/ migration中生成一个文件,您可以使用“rake db:migrate”应用该文件

Then you should modify your views to add the appropriate code to display/edit the new field.

然后,您应该修改视图,以添加显示/编辑新字段的适当代码。

more info: http://railscasts.com/episodes/83-migrations-in-rails-2-0

更多信息:http://railscasts.com/episodes/83-migrations-in-rails-2-0。

#2


0  

rails g scaffold Users name:string

If i have to add new field in already generated scaffold,

如果我必须在已经生成的支架上添加新字段,

rails generate migration add_url_to_users url:string

and modify the _form.htm.erb file as

和修改_form.htm。erb文件

<div class="field">
     <%= f.label :name %><br>
     <%= f.text_field :name %>
</div>

<div class="field">
     <%= f.label :url %><br>
     <%= f.text_field :url %>
</div>

But my problem is I can save name only not url

但是我的问题是我只能保存名字而不能保存url

#1


18  

Either you should re-run your generate scaffold/model (will blow away existing code)

您应该重新运行生成的scaffold/model(这会破坏现有的代码)

or you can add the field directly to the database and your view

或者可以直接将字段添加到数据库和视图中

Rails3: rails generate migration add_column_name_to_table_name column_name:string

Rails3: rails生成迁移add_column_name_to_table_name column_name:string

or

Rails2.x: ruby script/generate migration addColumnToTableName column_name:string

Rails2。x: ruby脚本/生成迁移的addColumnToTableName column_name:string

which will generate a file in db/migrate which you can apply with a 'rake db:migrate'

它将在db/ migration中生成一个文件,您可以使用“rake db:migrate”应用该文件

Then you should modify your views to add the appropriate code to display/edit the new field.

然后,您应该修改视图,以添加显示/编辑新字段的适当代码。

more info: http://railscasts.com/episodes/83-migrations-in-rails-2-0

更多信息:http://railscasts.com/episodes/83-migrations-in-rails-2-0。

#2


0  

rails g scaffold Users name:string

If i have to add new field in already generated scaffold,

如果我必须在已经生成的支架上添加新字段,

rails generate migration add_url_to_users url:string

and modify the _form.htm.erb file as

和修改_form.htm。erb文件

<div class="field">
     <%= f.label :name %><br>
     <%= f.text_field :name %>
</div>

<div class="field">
     <%= f.label :url %><br>
     <%= f.text_field :url %>
</div>

But my problem is I can save name only not url

但是我的问题是我只能保存名字而不能保存url