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

nee*_*raj 5 database ruby-on-rails scaffolding generated-code

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

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

我在这里谈论逆向工程

Fiv*_*ell 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
Run Code Online (Sandbox Code Playgroud)

或者你可以尝试 active_admin gem

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

rails generate active_admin:resource [MyModelName] 
Run Code Online (Sandbox Code Playgroud)

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

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

class Article < ActiveRecord::Base
  self.table_name "tbl_articles"
  self.primary_key "art_id"
end
Run Code Online (Sandbox Code Playgroud)