31

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?

BTW: question is general but can it be done quicker through Rubymine?

4 Answers 4

79

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No, it only adds the column to the db and the model when you run rake db:migrate. After created a scaffolding, there is no way to extend it automatically. As far as I know. It would be a complex task, way too complex. So you need to add the manually.
this should be the best answer!! Because you don't want to destroy and start over if there's already something.
35
  • 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.

Comments

5

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

Comments

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.