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
route resources :musics
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:
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:
== CreateMusics: migrating ===================================================
-> 0 rows
== CreateMusics: migrated (0.0790s) ==========================================
Step 10: Deploy your application
TorqueBox Server OK: C:/torquebox-1.0.0/jboss/server/default
Good! your application is finally completed!
Now start the JBoss AS which ships in the C:/torquebox-1.0.0/jboss folder.
If you point the browser to the application's home page you will see RoR welcome page:
Now point the browser to the musics folder :
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:
Once added the controller will redirect you to the list view which shows your record inserted:
Now you're done with it. You can experiment more about Ruby On Rails and Torquebox looking at the framework home page:
- << Prev