Ruby on Rails: Creating View And Controller Models from an Existing Database

Can I create controllers, models and view from an existing database?

I could not find the command over the search engine.

Here i'm talking about reverse engineering

+5
source share
3 answers

You need to create a simple model for each table with relationships, 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 must specify at least 2 rules for your model if they do not use rail agreements. Example

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

, . , , , , . , Rails , , .

guide, .

0

Here's how you can do it -

Try:

rails g scaffold myscaffold

This will create the files:

invoke  active_record
create    db/migrate/20130124100759_create_myscaffolds.rb
create    app/models/myscaffold.rb
invoke    test_unit
create      test/unit/myscaffold_test.rb
create      test/fixtures/myscaffolds.yml
 route  resources :myscaffolds
invoke  scaffold_controller
create    app/controllers/myscaffolds_controller.rb
invoke    erb
create      app/views/myscaffolds
create      app/views/myscaffolds/index.html.erb
create      app/views/myscaffolds/edit.html.erb
create      app/views/myscaffolds/show.html.erb
create      app/views/myscaffolds/new.html.erb
create      app/views/myscaffolds/_form.html.erb
invoke    test_unit
create      test/functional/myscaffolds_controller_test.rb
invoke    helper
create      app/helpers/myscaffolds_helper.rb
invoke      test_unit
create        test/unit/helpers/myscaffolds_helper_test.rb
invoke  assets

invoke    coffee
create      app/assets/javascripts/myscaffolds.js.coffee
invoke    scss
create      app/assets/stylesheets/myscaffolds.css.scss
invoke  scss
identical    app/assets/stylesheets/scaffolds.css.scss
0
source

All Articles