Ruby on Rails Monday, June 1, 2015

Thank you, byakugie ~

Your link led me to the gem "schema-to-scaffold", which enabled the following scenario:

The aim is to get all the components generated automatically, but based on the database schema.  I did it my using the gem schema_to_scaffold, which simply generates a script (one command in this case) that runs the scaffold generator in a way that drives off of the database schema for the new table (users).

1. Created a new database table, users:

sqlite> CREATE TABLE "users" ("id" INTEGER PRIMARY KEY

AUTOINCREMENT NOT NULL, "name" text, "email" textuser);

sqlite> INSERT INTO users (name) VALUES ('Jo');

sqlite> SELECT * FROM users;

1|Jo|

2. Installed schema_to_scaffold gem:

sqlite> gem install schema_to_scaffold

3. Ran schema_to_scaffold for users:

[sunward@web324 x]$ scaffold -p db

Looking for schema.rb in db

0. db/schema.rb

Select a path to the target schema:

Loaded tables:

0. toys

1. users

Options are:

4 for table 4; (4..6) for table 4 to 6; [4,6] for tables 4 and 6; * for all Tables

Select a table: 1

 

Script for scaffold:

rails generate scaffold User name:text email:text --no-migration

4. Ran the script produced by schema_to_scaffold:

[sunward@web324 x]$ rails generate scaffold User name:text email:text --no-migration

      invoke  active_record

      create    app/models/user.rb

      invoke    test_unit

      create      test/models/user_test.rb

      create      test/fixtures/users.yml

      invoke  resource_route

       route    resources :users

      invoke  scaffold_controller

      create    app/controllers/users_controller.rb

      invoke    erb

      create      app/views/users

      create      app/views/users/index.html.erb

      create      app/views/users/edit.html.erb

      create      app/views/users/show.html.erb

      create      app/views/users/new.html.erb

      create      app/views/users/_form.html.erb

      invoke    test_unit

      create      test/controllers/users_controller_test.rb

      invoke    helper

      create      app/helpers/users_helper.rb

      invoke      test_unit

      invoke    jbuilder

      create      app/views/users/index.json.jbuilder

      create      app/views/users/show.json.jbuilder

      invoke  assets

      invoke    coffee

      create      app/assets/javascripts/users.coffee

      invoke    scss

      create      app/assets/stylesheets/users.scss

      invoke  scss

      create    app/assets/stylesheets/scaffolds.scss

5. It works!  See http://x.sunward.webfactional.com/users/.

Amazing!

Next step will be to make a change in the database schema and see if/how I can get Rails to automagically update its model, controller, and views to fit. 

Any how-to hints would be welcome.

~ Ken

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fc6eb350-f39e-4597-9910-a7045c9b8c95%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment