如何在Ruby on Rails中添加字段?

时间:2022-10-31 23:01:15

I'm new to RoR, and I've just used scaffold to generate a table and create the pages for CRUD operations. Now I want to add a new field to this. One place I found tells me how to do that in the database, but is there a way to do it where it will add the field to all the pages too, or is that just a manual operation and I need to make sure I know all my fields up front?

我是RoR的新手,我刚刚使用scaffold生成一个表并为CRUD操作创建页面。现在我想为此添加一个新字段。我发现的一个地方告诉我如何在数据库中做到这一点,但有没有办法在它将所有页面添加字段,或者只是一个手动操作,我需要确保我知道所有我的领域在前面?

4 个解决方案

#1


13  

To add a new column to the database

向数据库添加新列

$ script/generate migration add_fieldname_to_tablename fieldname:string 
$ rake db:migrate

To get your views up to date you can run the scaffold again, with your updated list of fields. It will balk on replacing your migrations but you can force it to replace your views.

要使您的视图保持最新,您可以使用更新的字段列表再次运行脚手架。它将不再替换您的迁移,但您可以强制它替换您的视图。

$ script/generate scaffold tablename fieldname:string old_field_1:string ...

At the prompt answer a and it will overwrite the views, but not the old migration. It also won't modify your existing data.

在提示符处回答a并且它将覆盖视图,但不会覆盖旧的迁移。它也不会修改您现有的数据。

#2


3  

First you'll write a migration to add the field, run the migration, then you need to rerun the scaffold to regenerate the views, etc. Beware, this will wipe out edited files from before. Of course, instead of scaffolding again you could manually add references to new field where appropriate.

首先,您将编写一个迁移来添加字段,运行迁移,然后您需要重新运行脚手架以重新生成视图等。请注意,这将从之前删除已编辑的文件。当然,您可以在适当的时候手动添加对新字段的引用,而不是再次使用scaffolding。

#3


1  

You will have to update your database, no matter what (Remember to 'rake db:migrate' after you create the migration!)

无论如何,您都必须更新数据库(记住在创建迁移后'rake db:migrate'!)

Regarding the interface, you are a bit more lucky: the formtastic plugin makes your views look like this:

关于界面,你有点幸运:formtastic插件使你的视图看起来像这样:

The 'f.inputs' is calculating the form fields on-the-fly, based on your model's attributes. This will not cover complex forms that need special treatment, but the usual ones, you will get them automatically.

'f.inputs'根据您的模型属性即时计算表单字段。这不包括需要特殊处理的复杂形式,但通常情况下,您将自动获得它们。

For an easy-to-understand tutorial, see the latest railcast (Railscast #184, you will have to google for it, I can't post 2 links because I'm not cool enough for * yet, sorry).

对于一个易于理解的教程,请参阅最新的铁路广播(Railscast#184,你将不得不谷歌,我不能发布2个链接,因为我还不够酷,无法使用*,对不起)。

Railcast #185 is supposed to continue covering formtastic, and it's due to be published next monday.

铁路#185应该继续覆盖formtastic,它将于下周一发布。

#4


0  

Needs to be done manually, or the scaffold needs to be regenerated.

需要手动完成,或者需要重新生成脚手架。

#1


13  

To add a new column to the database

向数据库添加新列

$ script/generate migration add_fieldname_to_tablename fieldname:string 
$ rake db:migrate

To get your views up to date you can run the scaffold again, with your updated list of fields. It will balk on replacing your migrations but you can force it to replace your views.

要使您的视图保持最新,您可以使用更新的字段列表再次运行脚手架。它将不再替换您的迁移,但您可以强制它替换您的视图。

$ script/generate scaffold tablename fieldname:string old_field_1:string ...

At the prompt answer a and it will overwrite the views, but not the old migration. It also won't modify your existing data.

在提示符处回答a并且它将覆盖视图,但不会覆盖旧的迁移。它也不会修改您现有的数据。

#2


3  

First you'll write a migration to add the field, run the migration, then you need to rerun the scaffold to regenerate the views, etc. Beware, this will wipe out edited files from before. Of course, instead of scaffolding again you could manually add references to new field where appropriate.

首先,您将编写一个迁移来添加字段,运行迁移,然后您需要重新运行脚手架以重新生成视图等。请注意,这将从之前删除已编辑的文件。当然,您可以在适当的时候手动添加对新字段的引用,而不是再次使用scaffolding。

#3


1  

You will have to update your database, no matter what (Remember to 'rake db:migrate' after you create the migration!)

无论如何,您都必须更新数据库(记住在创建迁移后'rake db:migrate'!)

Regarding the interface, you are a bit more lucky: the formtastic plugin makes your views look like this:

关于界面,你有点幸运:formtastic插件使你的视图看起来像这样:

The 'f.inputs' is calculating the form fields on-the-fly, based on your model's attributes. This will not cover complex forms that need special treatment, but the usual ones, you will get them automatically.

'f.inputs'根据您的模型属性即时计算表单字段。这不包括需要特殊处理的复杂形式,但通常情况下,您将自动获得它们。

For an easy-to-understand tutorial, see the latest railcast (Railscast #184, you will have to google for it, I can't post 2 links because I'm not cool enough for * yet, sorry).

对于一个易于理解的教程,请参阅最新的铁路广播(Railscast#184,你将不得不谷歌,我不能发布2个链接,因为我还不够酷,无法使用*,对不起)。

Railcast #185 is supposed to continue covering formtastic, and it's due to be published next monday.

铁路#185应该继续覆盖formtastic,它将于下周一发布。

#4


0  

Needs to be done manually, or the scaffold needs to be regenerated.

需要手动完成,或者需要重新生成脚手架。