向Rails模型添加新字段

时间:2020-12-14 11:50:47

I already created a scafold using

我已经创建了一个scafold使用

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更快地完成吗?

4 个解决方案

#1


29  

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

    如果您刚刚生成它并意识到您的错误,您可以使用:

    rails destroy scaffold myOdb2

    rails destroy scaffold myOdb2

    and then re-generate the scaffolding:

    然后重新生成脚手架:

    rails generate scaffold myOdb2 columna:integer, columnB:string, columnc:string

    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 destroy scaffold_controller myOdb2

    rails destroy scaffold_controller myOdb2

    then create a migration to add the column:

    然后创建一个迁移来添加列:

    rails generate migration add_columnc_to_myodb2s columnc:string

    rails生成迁移add_columnc_to_myodb2s columnc:string

    then re-generate the controller and views:

    然后重新生成控制器和视图:

    rails generate scaffold_controller myOdb2 columna:integer, columnB:string, columnc:string

    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.

    如果您对控制器或视图进行了更改,则只需运行迁移即可更新数据库和模型,然后手动将新列添加到每个视图中。

#2


60  

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.

这会将列添加到您的db表中,当然也会添加到您的模型中,但不会添加到任何视图中。您需要手动添加它。据我所知。

#3


3  

no one mentioned updating strong parameters :

没有人提到更新强参数:

So , let us say I have an existing scaffold called myapp and 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

#4


1  

Scaffolding, quick and easy, generates data model and web interface all in once. However, rails generate scaffold is just a way to get started with your model and it helps just at the beginning.

脚手架,快速简便,一次性生成数据模型和Web界面。但是,rails生成脚手架只是开始使用模型的一种方式,它在一开始就有帮助。

Normally, you first have to extend the data model. This task is simplified by using rails generate migration and rake db:migration. Note that you may prefer to use rake with bundle exec to ensure to use the version of rake in your Gemfile.

通常,您首先必须扩展数据模型。使用rails generate migration和rake db:migration简化了此任务。请注意,您可能更喜欢使用rake with bundle 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 scaffolded model you may want to update the index and show views (see the app/views folder) and the myOdb2 controller (see the app/controllers folder)

例如,在全新的脚手架模型中,您可能需要更新索引并显示视图(请参阅app / views文件夹)和myOdb2控制器(请参阅app / controllers文件夹)

Do not forget to read about migratons http://guides.rubyonrails.org/migrations.html

不要忘记阅读有关迁移的信息http://guides.rubyonrails.org/migrations.html

#1


29  

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

    如果您刚刚生成它并意识到您的错误,您可以使用:

    rails destroy scaffold myOdb2

    rails destroy scaffold myOdb2

    and then re-generate the scaffolding:

    然后重新生成脚手架:

    rails generate scaffold myOdb2 columna:integer, columnB:string, columnc:string

    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 destroy scaffold_controller myOdb2

    rails destroy scaffold_controller myOdb2

    then create a migration to add the column:

    然后创建一个迁移来添加列:

    rails generate migration add_columnc_to_myodb2s columnc:string

    rails生成迁移add_columnc_to_myodb2s columnc:string

    then re-generate the controller and views:

    然后重新生成控制器和视图:

    rails generate scaffold_controller myOdb2 columna:integer, columnB:string, columnc:string

    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.

    如果您对控制器或视图进行了更改,则只需运行迁移即可更新数据库和模型,然后手动将新列添加到每个视图中。

#2


60  

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.

这会将列添加到您的db表中,当然也会添加到您的模型中,但不会添加到任何视图中。您需要手动添加它。据我所知。

#3


3  

no one mentioned updating strong parameters :

没有人提到更新强参数:

So , let us say I have an existing scaffold called myapp and 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

#4


1  

Scaffolding, quick and easy, generates data model and web interface all in once. However, rails generate scaffold is just a way to get started with your model and it helps just at the beginning.

脚手架,快速简便,一次性生成数据模型和Web界面。但是,rails生成脚手架只是开始使用模型的一种方式,它在一开始就有帮助。

Normally, you first have to extend the data model. This task is simplified by using rails generate migration and rake db:migration. Note that you may prefer to use rake with bundle exec to ensure to use the version of rake in your Gemfile.

通常,您首先必须扩展数据模型。使用rails generate migration和rake db:migration简化了此任务。请注意,您可能更喜欢使用rake with bundle 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 scaffolded model you may want to update the index and show views (see the app/views folder) and the myOdb2 controller (see the app/controllers folder)

例如,在全新的脚手架模型中,您可能需要更新索引并显示视图(请参阅app / views文件夹)和myOdb2控制器(请参阅app / controllers文件夹)

Do not forget to read about migratons http://guides.rubyonrails.org/migrations.html

不要忘记阅读有关迁移的信息http://guides.rubyonrails.org/migrations.html