I'm using Mac OSX version 10.5 for development but I assume it would not be very different on other OS like Windows.
I like to do all the installation in directory /usr/local. You can go to this directory from the terminal or from finder. Just keep in mind that this location is not visible from the finder window as the folder /usr is hidden by default. Click here to see how to open this window in finder.
First we need to install the latest version of JRuby. The zip file can be downloaded from here.
Once downloaded extract and move the jRuby folder to /usr/local
>mv /Users/sureshsharma/Downloads/jruby-1.3.1 /usr/local/jrub
After that go to the ~/.profile file and update the path for the installation
export PATH=/usr/local/jruby/bin:/usr/local/bin:/usr/sbin:$PATH
Now install rails, mongrel and jdbc adapter.If you are using sqlite3 instead of mysql then you must install the jdbc-sqlite3 adapter.
>sudo /usr/local/jruby/bin/gem install rails mongrel activerecord-jdbcsqlite3-adapter rspec rack rake jruby-openssl activesupport
At this point you have the following things installed.
- JRuby
- Rails framework
- mongrel server
- jdbc sqlite3 adapter
Now we are ready to go and create our hello world app.
Create a workspace directory to create the jruby project. In my case it is /Projects.
Once inside give the following command
/Projects>jruby -S rails helloWorldPrj
This command will create a project helloWorldPrj inside /Projects folder.
Ok now lets create a controller.
/Projects/helloWorldPrj>jruby -S script/generate controller home
The above command will create the following for you:
- A controller called HomeController in /helloWorldPrj/app/controllers/home_controller.rb
- A view Helper called HomeHelper in /helloWorldPrj/app/helpers/home_helper.rb
- a view folder in /helloWorldPrj/app/views/home
Now open your favorite editor to proceed. In my case I'm using Rad-Rails editor plugin for Eclipse. Go to the install and update new softwares option and give the following url http://update.aptana.com/install/rails/
Make sure you install only the RadRails editor infrastructure and nothing more than that. We will add on the stuff as and when we need it.
Once it is installed change to RadRails perspective and in the Ruby explorer
right click->new Project->Rails Project
Yes, you need to create new Rails Project even though we are going to use our existing helloWorldPrj project.
In the New project window uncheck both the following two options
- Generate Rails Application skeleton
- Automatically start server after project is created
Type in the name helloWorldPrj in the project name and press ok. This will automatically import the existing project that we created from the terminal into the Ruby explorer.
Open /helloWorldPrj/app/controllers/home_controller.rb file and create an action called view
class HomeController < ApplicationController
def view
@model = "Hello world!!!"
end
end
Next go inside the /helloWorldPrj/app/views/home folder and create a view.html.erb file. This will be our view template.
Inside the body tag give the following
<body>This is my first application!!!
body>
Now the next most important thing; because we are using sqlite3, go to /helloWorldPrj/config/database.yml and change the development,test and production adapters to jdbcsqlite3
/helloWorldPrj/config/database.yml file should look like this
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: jdbcsqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: jdbcsqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: jdbcsqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
We are done now!!! Go inside the titi project and start the server
/Projects/helloWorldPrj>jruby -S script/server
Now go the browser and check this out at http://0.0.0.0:3000/
You should see something like this
Now check out your hello world page at http://0.0.0.0:3000/home/view
Congratulations!!! You have just created your first Hello world jruby web application :-)
No comments:
Post a Comment