Miles to go ...

Arun Gupta is a Technology Evangelist for Web Services and Web 2.0 Apps at Sun. He was the spec lead for APIs in the Java platform, committer in multiple Open Source projects, participated in standard bodies and contributed to Java EE and SE releases.
« Screencast #23:... | Main | Lunar Eclipse in... »

http://blogs.sun.com/arungupta/date/20080220 Wednesday February 20, 2008

TOTD #28: Getting Started with Rails 2.0 Scaffold

Rails 2.0 changes the way Scaffold works. This blog walks you through the steps to create a simple CRUD application using Scaffold in Rails 2.0.

  1. Download & Install JRuby 1.1 RC2.
  2. Install Rails using the following command:

    jruby -S gem install rails
  3. Create a new Rails app using the following command:

    cd samples; mkdir rails; cd rails
    jruby -S rails books -d mysql
  4. Start MySQL server in a different shell using the following command:

    sudo /usr/local/mysql/bin/mysqld_safe --console
  5. Creat the database using the following command:

    cd books
    jruby -S rake db:create

    This creates the database defined by RAILS_ENV (Development is default). Here are some other new database-related commands:

    db:create:all Create all the databases (_Development, _Test, _Production)
    db:drop Drops your database
    db:reset Drop and Re-create your database, including migrations
  6. Generate a scaffold using the following command:

    jruby script/generate scaffold book title:string author:string isbn:string description:text


    The output of the command looks like:

          exists  app/models/
          exists  app/controllers/
          exists  app/helpers/
          create  app/views/books
          exists  app/views/layouts/
          exists  test/functional/
          exists  test/unit/
          create  app/views/books/index.html.erb
          create  app/views/books/show.html.erb
          create  app/views/books/new.html.erb
          create  app/views/books/edit.html.erb
          create  app/views/layouts/books.html.erb
          create  public/stylesheets/scaffold.css
      dependency  model
          exists    app/models/
          exists    test/unit/
          exists    test/fixtures/
          create    app/models/book.rb
          create    test/unit/book_test.rb
          create    test/fixtures/books.yml
          create    db/migrate
          create    db/migrate/001_create_books.rb
          create  app/controllers/books_controller.rb
          create  test/functional/books_controller_test.rb
          create  app/helpers/books_helper.rb
          route  map.resources :books


    There is no need to create the model explicitly as was the case in previous version of Rails. This creates the "db/migrate/001_create_books.rb" migration which looks like:

    class CreateBooks < ActiveRecord::Migration
      def self.up
        create_table :books do |t|
          t.string :title
          t.string :author
          t.string :isbn
          t.text :description

          t.timestamps
        end
      end

      def self.down
        drop_table :books
      end
    end
  7. Create the database tables using the following command:

    jruby -S rake db:migrate
  8. Deploy the application on WEBrick using the following command:

    jruby script/server


    The application is now available at "http://localhost:3000/books" and looks like:

    Rails2 CRUD Blank Page
  9. Click on "New book" to see a page as shown below (with values entered):

    Rails2 CRUD New Entry
    Click on Create button. After 2 entries have been entered, it looks like as shown below:

    Rails 2 CRUD Multiple Entries
That's it, you've created  a simple Rails 2.0 CRUD application.

You can also deploy this application easily on GlassFish v3 gem. Just follow the instructions here and enjoy!

I'll post a follow up blog where this is much more simplifed using NetBeans 6.1 builds where JRuby 1.1 and Rails 2.0.2 are already integrated.

Technorati: totd ruby jruby rubyonrails rails2 scaffold crud netbeans glassfish v3 gem

del.icio.us | furl | simpy | slashdot | technorati | digg
Comments:

[Trackback] Pramod announced the release of GlassFish v3 Gem ver 0.1.2. The focus of this release is bugfixes: IssueTracker #4228: Duplicate entries for Scaffold generated by Rails 2.0.2 IssueTracker #3966: Blank page returned if no Rails instance is available Ru...

Posted by Arun Gupta's Blog on March 13, 2008 at 08:23 PM PDT #

[Trackback] JRuby 1.1 is released and ready for production use today. You can unleash the potential in 3 simple steps - download, unzip &amp; get started. Here is a quote from the announcement: more and more reports of applications exceeding Ruby...

Posted by Arun Gupta's Blog on April 09, 2008 at 09:02 AM PDT #

[Trackback] Rails 2.1 was released earlier this month. GlassFish provides a complete development/deployment environment for Rails applications. Some of the main reasons for using GlassFish (instead of WEBrick or Mongrel) are: Identical Development and Deployment ...

Posted by Arun Gupta's Blog on June 19, 2008 at 06:46 AM PDT #

[Trackback] The default database for Rails 2.0.x application is&nbsp;SQLite3. This database is bundled with Mac OSX Leopard and so makes it really easy to get started with Ruby-on-Rails. But it requires couple of additional steps if you are using JRuby....

Posted by Arun Gupta's Blog on July 02, 2008 at 06:24 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
« Screencast #23:... | Main | Lunar Eclipse in... »

Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.

--> ajax ajaxworld conf eclipse fitness gem glassfish glassfishday hyderabad india indigo interoperability javaone javaone2008 jax-ws jmaki jpa jruby mac marathon metro microsoft mysql netbeans phobos photography presos railsconf ruby rubyonrails running runninglog runsfm screencast siliconvalleymarathon sun suntechdays swdp tango theserverside totd training traveltips v3 vista wcf web2.0 webservices webtier windows wsaddressing wsit youtube
Locations of visitors to this page

calendar

« September 2008
SunMonTueWedThuFriSat
 
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    
       
Today
www.flickr.com
This is a Flickr badge showing public photos from ArunGupta. Make your own badge here.
Add to Technorati Favorites

Last 50