Tim Santeford
Tim Santeford

Reputation: 28121

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.

Upvotes: 7

Views: 14085

Answers (4)

Hiro Ferreira
Hiro Ferreira

Reputation: 376

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!

Upvotes: 19

frenesim
frenesim

Reputation: 703

I think you can use this to solve part of your problem. After pointing your rails app to your old database then do a rake db:schema:dump you can use a gem called schema_to_scaffold to generate a scaffold script. It outputs:

rails g scaffold users fname:string lname:string bdate:date email:string encrypted_password:string

from your schema.rb our your renamed schema.rb. Check here

Upvotes: 3

Ryan
Ryan

Reputation: 11706

I believe this question is answered here: How to run `rails generate scaffold` when the model already exists? and here: rails g scaffold for existing model and DB table

with the short answer being (e.g. model = Post)

rails generate scaffold_controller Post

Upvotes: 4

Bashar Abdullah
Bashar Abdullah

Reputation: 1545

RailsAdmin gem https://github.com/sferik/rails_admin

Upvotes: 3

Related Questions