5

是否可以从现有数据库创建控制器、模型和视图?

我无法通过谷歌搜索找到命令。

这里我说的是逆向工程

4

3 回答 3

2

您必须为每个具有关系的表创建简单模型,然后您可以

[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

或者,您可以尝试 active_admin gem

活动管理员 -https://github.com/gregbell/active_admin

rails generate active_admin:resource [MyModelName] 

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

如果您的模型不使用 rails 约定,您应该为它指定至少 2 条规则。例子

class Article < ActiveRecord::Base
  self.table_name "tbl_articles"
  self.primary_key "art_id"
end
于 2013-01-24T10:26:57.780 回答
0

好吧,这违反了原则。如果您想要快速引导您的应用程序,您必须做的越好,那就是复制您在数据库中拥有的模型并使用脚手架。请记住,Rails 使用很多约定,如果您决定不遵循,您将遇到很多麻烦。

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

于 2013-01-24T10:08:53.277 回答
0

这就是你可以做到的 -

尝试:

rails g scaffold myscaffold

这将生成文件:

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
于 2013-01-24T10:09:21.713 回答