Step 8: Create your first application using scaffold 

Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.

 

 

 

Let's see what it can do for us.

 

We will create a sample "Music" application which store information about music tracks.

Issue the following command:

C:\torquebox-1.0.0\example>rails generate scaffold Music title:string description:text price:decimal
invoke  active_record
create    db/migrate/20110510184902_create_musics.rb
create    app/models/music.rb
invoke    test_unit
create      test/unit/music_test.rb
create      test/fixtures/musics.yml
route  resources :musics
invoke  scaffold_controller
create    app/controllers/musics_controller.rb
invoke    erb
create      app/views/musics
create      app/views/musics/index.html.erb
create      app/views/musics/edit.html.erb
create      app/views/musics/show.html.erb
create      app/views/musics/new.html.erb
create      app/views/musics/_form.html.erb
invoke    test_unit
create      test/functional/musics_controller_test.rb
invoke    helper
create      app/helpers/musics_helper.rb
invoke      test_unit
create        test/unit/helpers/musics_helper_test.rb
invoke  stylesheets
identical    public/stylesheets/scaffold.css


As you can see, the scaffold utility has created for you a set of directories which contain the basics of a MVC application which stores data in the music table. Following here is the explorer view of the example folder:

torquebox tutorial jboss jruby

The most interesting part is inside the app folder which contains:

 

  • a model where persistence information is stored (It's the equivalent of JPA)
  • a controller which redirects the views using its logic (It's the equivalent of the Servlet Controller)
  • a view which contains the pages which are displayed to the user (It's the equivalent of the JSPs)


Step 9: Reverse engineer to the database 

Now, the last step is synchronizing the model with the database. The following command will create the required table "musics" on mysql:

C:\torquebox-1.0.0\example>rake db:migrate
(in C:/torquebox-1.0.0/example)
==  CreateMusics: migrating ===================================================
-- create_table(:musics)
-> 0.0790s
-> 0 rows
==  CreateMusics: migrated (0.0790s) ==========================================


Step 10: Deploy your application 

C:\torquebox-1.0.0\example>rake torquebox:deploy['/example']
(in C:/torquebox-1.0.0/example)
TorqueBox Server OK: C:/torquebox-1.0.0/jboss/server/default
Deployed: example-knob.yml
into: C:/torquebox-1.0.0/apps


Good! your application is finally completed!
Now start the JBoss AS which ships in the C:/torquebox-1.0.0/jboss folder.
run.cmd

If you point the browser to the application's home page you will see RoR welcome page:
http://localhost:8080/example/
ruby on rails jruby torquebox tutorial

Now point the browser to the musics folder :

 

http://localhost:8080/example/musics/

 

torquebox tutorial jboss ruby on rails

 

An empty list will appear. Let's add something to it, by clicking on the "New Music" link.

You will be redirected to the view that handles form data insert:


ruby on rails jruby tutorial

torquebox tutorial ruby on rails jboss


Once added the controller will redirect you to the list view which shows your record inserted:


ruby on rails jruby torquebox tutorial
Now you're done with it. You can experiment more about Ruby On Rails and Torquebox looking at the framework home page:
http://torquebox.org

0
0
0
s2smodern