سؤال

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

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

Check this guide if you need help.

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top