TOTD #28: Getting Started with Rails 2.0 Scaffold
Rails
2.0 changes the way
href="http://wiki.rubyonrails.org/rails/pages/Scaffold">Scaffold
works. This blog walks you through the steps to create a simple CRUD
application using Scaffold in Rails 2.0.
- Download & Install href="http://dist.codehaus.org/jruby/">JRuby 1.1 RC2.
- Install Rails using the following command:
jruby -S gem install rails - Create a new Rails app using the following command:
cd samples; mkdir rails; cd rails<br>
jruby -S rails books -d mysql - Start MySQL server in a different shell using the following
command:
sudo /usr/local/mysql/bin/mysqld_safe --console - 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:
border="0" cellpadding="2" cellspacing="2">
db:create:allCreate all the databases (_Development, _Test,
_Production)db:dropDrops your database db:resetDrop and Re-create your database, including
migrations - 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>
exists
app/models/<br>
exists
app/controllers/<br>
exists
app/helpers/<br>
create
app/views/books<br>
exists
app/views/layouts/<br>
exists
test/functional/<br>
exists
test/unit/<br>
create
app/views/books/index.html.erb<br>
create
app/views/books/show.html.erb<br>
create
app/views/books/new.html.erb<br>
create
app/views/books/edit.html.erb<br>
create
app/views/layouts/books.html.erb<br>
create
public/stylesheets/scaffold.css<br>
dependency model<br>
exists app/models/<br>
exists test/unit/<br>
exists test/fixtures/<br>
create app/models/book.rb<br>
create test/unit/book_test.rb<br>
create test/fixtures/books.yml<br>
create db/migrate<br>
create db/migrate/001_create_books.rb<br>
create
app/controllers/books_controller.rb<br>
create
test/functional/books_controller_test.rb<br>
create
app/helpers/books_helper.rb<br>
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<br>
def self.up<br>
create_table :books do |t|<br>
t.string :title<br>
t.string :author<br>
t.string :isbn<br>
t.text :description<br>
<br>
t.timestamps<br>
end<br>
end<br>
<br>
def self.down<br>
drop_table :books<br>
end<br>
end - Create the database tables using the following command:
jruby -S rake db:migrate - 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:
alt="Rails2 CRUD Blank Page" title="Rails2 CRUD Blank Page"
src="http://blogs.sun.com/arungupta/resource/ror/rails2-crud-blank-page.png">
- Click on "New book" to see a page as shown below (with
values entered):
alt="Rails2 CRUD New Entry" title="Rails2 CRUD New Entry"
src="http://blogs.sun.com/arungupta/resource/ror/rails2-crud-new-entry.png">
Click on Create button. After 2 entries
have been entered, it looks like as shown below:
alt="Rails 2 CRUD Multiple Entries"
title="Rails 2 CRUD Multiple Entries"
src="http://blogs.sun.com/arungupta/resource/ror/rails2-crud-multiple-entries.png">
That's it, you've created a simple Rails 2.0 CRUD application.
You can also deploy this application easily on
href="http://rubyforge.org/projects/glassfishgem/">GlassFish
v3 gem. Just follow the instructions
href="http://blogs.sun.com/arungupta/entry/glassfish_v3_gem_0_1">here
and enjoy!
I'll post a follow up blog where this is much more simplifed using
href="http://bits.netbeans.org/netbeans/trunk/nightly/latest/">NetBeans
6.1 builds where JRuby 1.1 and Rails 2.0.2 are already
integrated.
Technorati: totd
ruby
jruby
href="http://technorati.com/tag/rubyonrails">rubyonrails
rails2
scaffold
crud
href="http://technorati.com/tag/netbeans">netbeans
glassfish
v3
href="http://technorati.com/tag/gem">gem
- Login or register to post comments
- Printer-friendly version
- arungupta's blog
- 1283 reads






Comments
by arungupta - 2008-02-20 12:11
robertonrails, Glad you enjoyed it! Rails 2.0.2 is already bundled with latest NetBeans 6. I'm planning for a screencast/tutorial on that topic as well. -Arunby robertonrails - 2008-02-20 08:38
I am looking forward to seeing how to do JRuby on Rails 2.0.2 apps with NetBeans. Thanks for the great tutorials, as always.