Adding new column to table - browser not showing it

Hi, I'm trying to add a “Date available” column to a “Products” table.

Here’s what I typed in and got back:

ruby script/generate migration AddDate_availableToProducts date_available:datetime exists db/migrate create db/migrate/20080613112844_add_date_available_to_products.rb

The db/migrate file seems to have been updated correctly: class AddDateAvailableToProducts def self.up add_column :products, :date_available, :datetime end

def self.down remove_column :products, :date_available end end

The Products db (in phpmyadmin) is showing the new column along with the existing ones. But the browser (http://localhost:3000/products) isn't updating with the new column - it's just showing the existing fields. I also ran rake db:migrate in case this would help and here's what it returned:

== 20080613112844 AddDateAvailableToProducts: migrating

The Products db (in phpmyadmin) is showing the new column along with
the existing ones. But the browser (http://localhost:3000/products) isn't updating with the new column - it's just showing the existing
fields. I also ran rake db:migrate in case this would help and here's what it returned:

== 20080613112844 AddDateAvailableToProducts: migrating

-- add_column(:products, :date_available, :datetime)   -> 0.5930s == 20080613112844 AddDateAvailableToProducts: migrated (0.5930s)

Any idea where I am going wrong? Thanks.

Nowhere. if you looked at the view files in your rails app you'd see
it's just a static list of columns: you need to update the views.

Fred

Frederick Cheung wrote:

Nowhere. if you looked at the view files in your rails app you'd see it's just a static list of columns: you need to update the views.

Fred

Thanks for your reply Fred. Sorry, but I'm not sure what you
mean...how can I update the views?

You edit the corresponding files.

Fred

Frederick Cheung wrote:

Nowhere. if you looked at the view files in your rails app you'd see it's just a static list of columns: you need to update the views.

Fred

Thanks for your reply Fred. Sorry, but I'm not sure what you
mean...how can I update the views?

You edit the corresponding files.

Fred

I presume what Fred means is to edit all the files in views/products. Well I haven't actually written any code yet for this application, it has all been generated with the scaffold command. Since the application is still in the very early stages, is there not just a way to update the views with the scaffold again?

Once you have changed your table schema running a migration that added a new column to it, you can re-rerun the scaffold generation. It will ask you if it should replace existing files.

To answer your above question, I think there is a plugin which contains the old facility for having a dynamic scaffold. However, just running the scaffold again will do the trick or simply looking through the code and reusing the examples you already have. I would take the latter route because you'll get a better feel for how Rails handles things and how the code is put together.

YES , Bobnation is absolutely correct, the scaffold is nothing but creating CRUD depending upon the columns in the database. WHat Scaffold does is it maps the columns in database and created CRUD for it. So if you have added altered table by adding new column then replace those files by re-scaffolding.

Hey try using active scaffold , its much better ...........

Hey guys, thanks for the very useful advice.

I tried editing the views and overwriting the scaffolding but received some errors (see below for details). So I destroyed the existing scaffold and generated a new one of the same name and now its working fine.

Here are the errors I received: 1. When updating the views, there was a problem using the "datetime" data type for the "Date Available" field. On the Create/Update pages, the browser showed:

"NoMethodError in Products#edit Showing products/edit.html.erb where line #28 raised: undefined method `datetime' for #<ActionView::Helpers::FormBuilder:0x383a59c>"

2. When I tried to overwrite the existing Scaffold (ie. ruby script/generate scaffold Product), Command Prompt showed:

"The name 'ProductsHelper' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again."

Its working now, but out of curiosity - any idea why these 2 errors happened?

Thanks very much for your help.

ruby script/scaffold no longer looks at the db to build the pages. It looks at what’s on the commandline passed as arguments. So running scaffold again can break stuff because it will try to create a migration that’s already there unless you tell it to skip that part.

You can redo those files with

ruby script/scaffold project name:string description:text started_on:datetime ended_on:datetime estimated_hours:decimal --skip-migration

But generally, it’s considered best practice to not use scaffolding at all and get comfortable actually working with the form helpers. Adding a column is trivial.

If you’re really lazy (like me) you could install my gem called scaffold_form

sudo gem install scaffold_form_generator

This generator creates the new and edit pages by looking at your database table directly, the way Rails used to do it. It doesn’t do the list or the show pages because I didn’t think that was necessary. Form generation is nice because it’s often time-consuming.

However, this is still cheating, and not meant for production. :slight_smile:

(pass the model and controller name to the generator) ruby script/generate scaffold_form Project projects

See http://scaffoldform.rubyforge.org/ for more.