Ruby On Rails:从现有数据库创建模型视图和控制器

时间:2022-06-15 07:34:56

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

我在这里谈论逆向工程

3 个解决方案

#1


2  

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

或者,您可以尝试使用active_admin gem

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

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

rails generate active_admin:resource [MyModelName] 

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

RailsAdmin也足够好了https://github.com/sferik/rails_admin

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

如果模型不使用rails约定,则应为模型指定至少2个规则。例

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

#2


0  

Well this goes against principles. The better you have to do, if you want a quick bootstrap for your application is replicate the models you have on your database and use scaffolding. Remember that Rails use a LOT of conventions, and if you decide not follow you'll have a lot of trouble.

这违背了原则。您必须做得越好,如果您希望为您的应用程序快速引导,则复制数据库中的模型并使用scaffolding。请记住,Rails使用了很多约定,如果你决定不遵循,你会遇到很多麻烦。

Check this guide if you need help.

如果您需要帮助,请查看本指南。

#3


0  

This is how you can do that -

这就是你如何做到的 -

Try:

尝试:

rails g scaffold myscaffold

This will generate 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

#1


2  

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

或者,您可以尝试使用active_admin gem

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

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

rails generate active_admin:resource [MyModelName] 

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

RailsAdmin也足够好了https://github.com/sferik/rails_admin

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

如果模型不使用rails约定,则应为模型指定至少2个规则。例

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

#2


0  

Well this goes against principles. The better you have to do, if you want a quick bootstrap for your application is replicate the models you have on your database and use scaffolding. Remember that Rails use a LOT of conventions, and if you decide not follow you'll have a lot of trouble.

这违背了原则。您必须做得越好,如果您希望为您的应用程序快速引导,则复制数据库中的模型并使用scaffolding。请记住,Rails使用了很多约定,如果你决定不遵循,你会遇到很多麻烦。

Check this guide if you need help.

如果您需要帮助,请查看本指南。

#3


0  

This is how you can do that -

这就是你如何做到的 -

Try:

尝试:

rails g scaffold myscaffold

This will generate 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