Regenerate Model / View for new columns

I am trying to learn Ruby on Rails, so this may be a pretty basic
question.

I have added new columns to an existing table using
'rails generate migration add_column_to_table' command.

I don't see any change in model & view due to this.

So, I changed the all the views (like _form.html.erb & index.html.erb)
to include a new form element like:
    <%= f.label :filename %><br>
    <%= f.text_field :filename %>

where :filename was the new column.

When I run the rails, while the new column comes on UI, it doesn't get
saved into db. I think the binding between View and Model is missing.

Is there a way to add a new column in the table to all layers
(view/controller/model etc) apart from the db.

I am trying to learn Ruby on Rails, so this may be a pretty basic
question.

I have added new columns to an existing table using
'rails generate migration add_column_to_table' command.

I don't see any change in model & view due to this.

So, I changed the all the views (like _form.html.erb & index.html.erb)
to include a new form element like:
   <%= f.label :filename %><br>
   <%= f.text_field :filename %>

where :filename was the new column.

When I run the rails, while the new column comes on UI, it doesn't get
saved into db. I think the binding between View and Model is missing.

Is there a way to add a new column in the table to all layers
(view/controller/model etc) apart from the db.

The scaffold generator only runs once, and it generates static files that you may edit yourself later. There was a time (in the pre -1.0 and 1.x era of Rails) when the scaffold was a live reflection of the database, and let you do things like you describe here -- change the database and the scaffold UI would change to reflect that. It hasn't worked that way for many years now.

If you open the Rails console, you will find that adding these columns to your database did create new methods in the model for you. If you added the filename to your database, then you would find that you could do YourModel.filename = "foo.bar" and that would work, and then you could read that back, and you could check if it had been set with filename? or save the model and get that value back later from the database. But you will need to add these fields to your form and your show and index page manually if you want to use them.

Like the dealer says, "first taste is free", and after that you have to break out your text editor and add some fields and outputs yourself.

Walter

Also, in Rails 4.x:
http://edgeguides.rubyonrails.org/action_controller_overview.html#strong-parameters

Thanks Hassan Schroeder & Walter Davis for the replies.
I had added the form element in view page.
But I had forgotten to add it to the "<object>_params" in the
controller.
So, the new element, although getting passed from UI wasn't getting into
the "create" methods. The issue is now fixed.

Thanks again.

I think what you might have missed is that rails generate only creates a migration file — it doesn’t actually put the instructions to do the migration into them migration file. It also doesn’t’ make any changes to your Model or View files (as you said).

look in db/migrate/ for a file that recently got created (it will have a timestamp like 20140902… and part of its name will be “add_column_to_table”)

If I’m right, when you open that file, you’ll see empty up and down methods.

You’ll want to add to the up method

add_column :users, :filename, :string

and to the down method

remove_column :users, :filename

(I just made up “users” since you didn’t specify what model this is)

Once you do that, run rake db:migrate (be sure to do rake db:migrate RAILS_ENV=test for your test environment) and also stop & restart your webserver too!

-Jason