Search code examples
ruby-on-railsdatabasescaffoldinggenerated-code

Ruby On Rails: Create Models View And Controller from existing database


Is it possible to create controllers, models and view from the existing database?

I could not find the command over googling.

Here i am talking about Reverse Engineering


Solution

  • You have to create simple model for every table with relations, and then you can

    [rails3] > rails generate scaffold_controller Club name:string exclusive:boolean
          create  app/controllers/clubs_controller.rb
          invoke  erb
          create    app/views/clubs
          create    app/views/clubs/index.html.erb
          create    app/views/clubs/edit.html.erb
          create    app/views/clubs/show.html.erb
          create    app/views/clubs/new.html.erb
          create    app/views/clubs/_form.html.erb
          create    app/views/layouts/clubs.html.erb
          invoke  test_unit
          create    test/functional/clubs_controller_test.rb
    

    Alternatively you can try active_admin gem

    ActiveAdmin - https://github.com/gregbell/active_admin

    rails generate active_admin:resource [MyModelName] 
    

    RailsAdmin is also good enough https://github.com/sferik/rails_admin

    You should specify at least 2 rules for your model if it doesn't use rails conventions. Example

    class Article < ActiveRecord::Base
      self.table_name "tbl_articles"
      self.primary_key "art_id"
    end