Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto generate scaffolding for an existing database scheme in Rails 3

I have existing database that I would like to auto generate the controllers and views for including models with field size and not null validation. I looked into a project called the Magic Model Generator but it looked old and only creates models from what I can tell.

I want this as primarily a time saver and I understand that most of the controllers and views will not be useful for user facing functionally. I will be plucking out what I don't need and moving most of it to administration pages.

Is there a tool for doing this?

TIA!

Edit: I want to create the actual files and do not want a dynamic admin. I will be using the controllers and view for some front facing parts of the site.

like image 458
Tim Santeford Avatar asked Mar 04 '11 00:03

Tim Santeford


People also ask

How do you generate scaffold in rails?

To generate a fully working scaffold for a new object, including model, controller, views, assets, and tests, use the rails g scaffold command. Then you can run rake db:migrate to set up the database table. Then you can visit http://localhost:3000/widgets and you'll see a fully functional CRUD scaffold.

What is the difference between static and dynamic scaffolding?

What is the difference between both static and dynamic Scaffolding? Static Scaffolding needs user insertion commands to generate data fields, whereas Dynamic Scaffolding generates the entire content at run time.

What is scaffolding in Ruby on rails?

Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.


1 Answers

Not sure if works for Rails 3, but does for Rails 4! Steps below teaches how to build your models, forms, controllers and etc from a existing database.

  1. Model and deploy your database, which yet has nothing to do with your rails app.
  2. Point your rails app config/database.yml to such database
  3. Open terminal
  4. Change directory to you rails app root (run in terminal cd /path/to/my/rails/app/) .
  5. Run in terminal rake db:schema:dump (Generates schema.rb)
  6. Install gem 'schema_to_scaffold' (https://github.com/frenesim/schema_to_scaffold/)
  7. Run in terminal scaffold to generate rails scaffold commands and follow what is requested
  8. copy "rails scaffold commands" generated by step 7 on a separated .txt file
  9. Run each "rails scaffold command" separately
  10. Be happy!

Hope it helps!

like image 141
Hiro Ferreira Avatar answered Sep 29 '22 09:09

Hiro Ferreira