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:
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:
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 ...........
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?
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.
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.
(pass the model and controller name to the generator)
ruby script/generate scaffold_form Project projects