Ruby-on-rails 向 Rails 模型添加新字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10352832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a new field to Rails model
提问by Bick
I already created a scafold using
我已经使用创建了一个脚手架
rails generate scaffold myOdb2 columna:integer, columnB:string
now I want to add columnc:string. What should I do?
现在我想添加columnc:string. 我该怎么办?
BTW: question is general but can it be done quicker through Rubymine?
顺便说一句:问题很普遍,但可以通过 Rubymine 更快地完成吗?
回答by Matzi
You must generate a migration:
您必须生成迁移:
rails g migration add_columnc_to_myodb2s columnc:string
It should contain a row of adding a column to your table.
它应该包含一行向您的表格添加一列。
add_column :myodb2s, :columnc, :string
This adds the column to yourdb table, and of course to your model, but not in any view. You need to add it manually. As far s I know.
这会将列添加到 yourdb 表中,当然也添加到您的模型中,但不在任何视图中。您需要手动添加它。据我所知。
回答by Nate
If you've just generated it and realized your mistake you can use:
rails destroy scaffold myOdb2and then re-generate the scaffolding:
rails generate scaffold myOdb2 columna:integer, columnB:string, columnc:stringIf 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 myOdb2then create a migration to add the column:
rails generate migration add_columnc_to_myodb2s columnc:stringthen re-generate the controller and views:
rails generate scaffold_controller myOdb2 columna:integer, columnB:string, columnc:stringIf 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 destroy scaffold myOdb2然后重新生成脚手架:
rails generate scaffold myOdb2 columna:integer, columnB:string, columnc:string如果您对要保留的脚手架创建的模型进行了一些更改,但不介意破坏控制器和视图:
rails destroy scaffold_controller myOdb2然后创建迁移以添加列:
rails generate migration add_columnc_to_myodb2s columnc:string然后重新生成控制器和视图:
rails generate scaffold_controller myOdb2 columna:integer, columnB:string, columnc:string如果您对控制器或视图进行了更改,则只需运行迁移以更新数据库和模型,然后手动将新列添加到每个视图中。
回答by zee
no one mentioned updating strong parameters :
没有人提到更新强参数:
So , let us say I have an existing scaffold called myappand I want to add more fields to that scaffold . Three things to be done .
因此,假设我有一个名为的现有脚手架myapp,我想向该脚手架添加更多字段。要做三件事。
The field to be added are :
要添加的字段是:
=>
=>
1) rails g migration add_term_count_and_current_record_count_and_previous_record_count_to_myapp term_count:integer , current_record_count:integer , previous_record_count:integer
1) rails g migration add_term_count_and_current_record_count_and_previous_record_count_to_myapp term_count:integer , current_record_count:integer , previous_record_count:integer
=>
=>
2) Update views, example updating _form.html.rb
I needed to add :
我需要添加:
<div class="field">
<%= f.label :current_record_count %><br>
<%= f.number_field :current_record_count%>
</div>
<div class="field">
<%= f.label :current_record_count %><br>
<%= f.number_field :previouse_record_count%>
</div>
<div class="field">
<%= f.label :term_count %><br>
<%= f.number_field :terminations_count %>
</div>
=>
=>
3) Update Controller :
New versions of rails has what is called strong parameter to prevent hackers from passing arbitrary column field values. Long story short , update the method with the new fields names , otherwise you will not see the new fields.. they wont get passed to anywhere...untrusted values ;o)
新版本的 rails 具有所谓的强参数,以防止黑客传递任意列字段值。长话短说,用新的字段名称更新方法,否则你将看不到新字段..它们不会被传递到任何地方......不受信任的值;o)
# Never trust parameters from the scary internet, only allow the white list through.
def vendor_file_params
params.require(:vendor_file).permit(:name, :run_date, :term_count ,
:current_record_count , :previous_record_count ,:comments)
end
end
回答by Emiliano Poggi
Scaffolding, quick and easy, generates data model and web interface all in once. However, rails generate scaffoldis just a way to get started with your model and it helps just at the beginning.
脚手架,快速简便,一次性生成数据模型和 Web 界面。然而,railsgenerate scaffold只是开始使用您的模型的一种方式,它只是在开始时有所帮助。
Normally, you first have to extend the data model. This task is simplified by using rails generate migrationand rake db:migration. Note that you may prefer to use rake with bundle execto ensure to use the version of rake in your Gemfile.
通常,您首先必须扩展数据模型。使用rails generate migration和可以简化此任务rake db:migration。请注意,您可能更喜欢使用 rake withbundle exec以确保在您的 Gemfile 中使用 rake版本。
Thereafter, you probably want to update (maybe also create new) controllers and views directly, according to the requirements of your web application.
此后,您可能希望根据 Web 应用程序的要求直接更新(也可能创建新的)控制器和视图。
aka MVC
又名MVC
For example, in brand new scaffoldedmodel you may want to update the index and show views (see the app/viewsfolder) and the myOdb2 controller (see the app/controllersfolder)
例如,在全新的脚手架模型中,您可能希望更新索引并显示视图(请参阅app/views文件夹)和 myOdb2 控制器(请参阅app/controllers文件夹)
Do not forget to read about migratons http://guides.rubyonrails.org/migrations.html
不要忘记阅读有关迁移的信息http://guides.rubyonrails.org/migrations.html

