Rake db:migrate and HTML generated not changing - ruby-on-rails

I'm very new to RoR and building my first application. I used rails generate scaffold and created a table in database. Of course I did it wrong, wanted to make changes to the table, did it by rewriting the generated migration file. I think rake db:migrate works fine, because it's updating my schema, but there are no changes visible on the site. The view of the table didn't change, although the mechanism is different, I can't add anything now because it can't find the proper columns after I changed them. I have no idea how to fix it without rewriting the view files myself. Is it possible, I think I'm missing something?

You can undo/destroy scaffold by rails destroy scaffold scaffold_name and then re-generate scaffold with the columns you added on migration file. FYI, editing migration files is not a good practice.

You have two options, either you destroy the scaffolding that you've created with rails destroy scaffold ModelName or you change the views by hand. When you run rake db:migrate it won't do anything in the views.

Related

Updating scaffold views after adding new column

I created a table using scaffold. Later added a new column
$ rails generate migration add_column_name_to_table_name column_name:string
$ rake db:migrate
Now I'm wondering is there any way to auto-update earlier generated by scaffold views?
Nope, you've got to do it manually.
FWIW, these sorts of changes are made much easier if you're using one of the form generator gems.

rails scaffold form, simplest way of adding a field?

Say I did rails g scaffold Review artist:string song:string genre:string and created a review object with those fields...would it later be possible to easily add another field like rating:int via a rails command, or would I need to manually edit all the files to include another data field for the form? I know it's possible to just delete the first scaffold and rebuild with the extra field, but I was just wondering if there was an easier way.
Thanks
The scaffold just generates minimalist model, view and controller code along with (usually) a database migration.
If you haven't made any serious changes to any of that code, it may be easier to rerun the scaffold generator. If you haven't run the database migrations, or you haven't committed them, you can add your attributes in the existing migration code and follow up with corresponding changes in the view.
If you've committed the code and someone is already depending on the existing model, you'll want to generate a database change migration to preserve a graceful upgrade/downgrade path.
The scaffold isn't magic, it's just a quick and dirty way of generating code that you can edit.

Is there a built in way to rename an entire scaffold?

I'm using Rails 3.2, and have started with a scaffolding and built out from it, but have realized I need to rename the entire scaffold (Model, View, Controller, db:migrate, etc). Is there a built in way to do this, or should I just do it manually?
I don't think there's anything rails provides to rename the name of models/controllers/views/tests etc. once they are created - whether as a part of a scaffold, or not.
You will have to change it manually.
If it is a brand new app that you have just started on, it might be easier to just delete the whole directory/drop the database, and start over again.
If not, you will have to go through the files created/modified by the scaffold generation, and modify them manually.
Make sure you either drop_and_recreate the relevant table, or add a migration to rename the table. See How do you write a migration to rename an ActiveRecord model and its table in Rails? for some relevant advice.
I think there's no out of the box method to rename files generated by scaffold.What you have to do is create a new scaffold and copy your codes from the old to the new.Copy contents from factories, model, controller and their respective spec file to the new scaffold. Then remove the old scaffold with the command
rspec d scaffold <Model Name>
and you have to create a new migration to drop that old table from the database. Then run your migrations.

Ruby on Rails : purpose of db:migrate

When I read Rails book, each time they create a new database, always follow a db:migrate.
rails generate scaffold school
rake db:migrate
In console view, I see at first line, Rails create some files, no problem. but in second line, I see that Rails isn't really change anything. I have view some files that Rails nearly create and see no change too.
So, what the purpose of line 2, please tell me.
Thanks :)
The rake migrates the changes into your database. It is which acttually changes the database schema to match your previously generated scaffolded model.
Without it, you wouldn't have a table to write your objects into. Or in case of changed model, the table could differ from your model, leading to error.
When you generate a model (or scaffold one) a migration file is created in your db/migration directory. It is a pure text file, you can create such manually, if you want. This is the tool for the iterative development in rails regarding the database. Each migration adds some change to the system. When you run rake db:migrate your database is updated by the given migrations. This is a handy tool in case of distributed development, when one programmer can check out the code from the repository, and can run the migrations on his own development database.
db:migrate, is the command that tells rails to update the database with new changes. Think of it as this way
when u say rails generate scaffold rails will generate files like a model, controller etc.. and it create a file under db/migrate which has the sql script to update the database.
Ex: if you run rails generate scaffold User name:string, then you will need a table called users in the database with the column 'name', that sql script will generated under db/migrate folder
with db:migrate, command, you are telling rails to migrate new sql scripts to the database, in the above case, it will creates the 'users' table
if you run rake -T, from your rails application root, you could see all the rake tasks
HTH :)

Re-Generating Scaffold

Is there any way to re-generate views from newly updated model?
Unless you've made a lot of changes to the scaffold, it's probably easier just to overwrite the current scaffold:
./script/generate -f scaffold Model
I had the same need, I just backed up some changes and did a destroy/generate, Take care, destroy deletes Everything related to the Model :
rails destroy scaffold Model -f
before I generate again.
rails generate scaffold Model
The scaffold is not intended to create the interface, only to make a starting point for further development. I'm not sure it's even possible (without a lot of unnecessary work) to redo a scaffold, it's much quicker to manually edit the view and controller anyway.

Resources