Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

http://blogs.sun.com/arungupta

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<br>
    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<br>
    jruby -S rake db:create<br>
       


    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:

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




    The output of the command looks like:

    <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exists&nbsp;
    app/models/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exists&nbsp;
    app/controllers/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exists&nbsp;
    app/helpers/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/views/books<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exists&nbsp;
    app/views/layouts/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exists&nbsp;
    test/functional/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exists&nbsp;
    test/unit/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/views/books/index.html.erb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/views/books/show.html.erb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/views/books/new.html.erb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/views/books/edit.html.erb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/views/layouts/books.html.erb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    public/stylesheets/scaffold.css<br>
    &nbsp; dependency&nbsp; model<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    exists&nbsp;&nbsp;&nbsp; app/models/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    exists&nbsp;&nbsp;&nbsp; test/unit/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    exists&nbsp;&nbsp;&nbsp; test/fixtures/<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    create&nbsp;&nbsp;&nbsp; app/models/book.rb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    create&nbsp;&nbsp;&nbsp; test/unit/book_test.rb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    create&nbsp;&nbsp;&nbsp; test/fixtures/books.yml<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    create&nbsp;&nbsp;&nbsp; db/migrate<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    create&nbsp;&nbsp;&nbsp; db/migrate/001_create_books.rb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/controllers/books_controller.rb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    test/functional/books_controller_test.rb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; create&nbsp;
    app/helpers/books_helper.rb<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; route&nbsp;
    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 &lt; ActiveRecord::Migration<br>
    &nbsp; def self.up<br>
    &nbsp;&nbsp;&nbsp; create_table :books do |t|<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.string :title<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.string :author<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.string :isbn<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.text :description<br>
        <br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.timestamps<br>
    &nbsp;&nbsp;&nbsp; end<br>
    &nbsp; end<br>
        <br>
    &nbsp; def self.down<br>
    &nbsp;&nbsp;&nbsp; drop_table :books<br>
    &nbsp; end<br>
    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:

    <br>
    jruby script/server




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




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





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


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