Adding a new field to Rails model

前端 未结 4 1919
臣服心动
臣服心动 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.

    0 讨论(0)
  • 2021-01-30 11:57

    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.

    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.

    Thereafter, you probably want to update (maybe also create new) controllers and views directly, according to the requirements of your web application.

    aka 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)

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

    0 讨论(0)
  • 2021-01-30 12:01

    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.

    0 讨论(0)
  • 2021-01-30 12:01

    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 .

    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

    =>

    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)

     # 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
    
    0 讨论(0)
提交回复
热议问题