How to install Ruby On Rails running Xampp on Windows (hello world, Ruby!)

Hohoi…i’m so interested to write a post on my struggles to get my first Ruby on Rails work on Windows.
Thanks to internet which provided so many Guru who want to share their knowledge and skills.
Finally, this was a lot simpler than i had figured.
You may install Apache2 on windows or you may use some Apache2 install packages.
I picked XAMPP beside AppServ, Apache2Triad, WAMP just because i like packages included on it.
So here some step that i use,

1. install Xampp from ApacheFriends (http://www.apachefriends.org/en/index.html),
for a simpler download (http://www.apachefriends.org/download.php?xampp-win32-1.6.6a-installer.exe).
I install Xampp at c:\xampp

2. install Ruby (ruby186-26.exe or new version) from RubyForge ((http://rubyforge.org/projects/rubyinstaller/),
download an easy one-click Ruby installer, a self-contained installer that includes the Ruby language,
dozens of popular extensions, a syntax-highlighting editor and the book “Programming Ruby: The Pragmatic Programmer’s Guide

3. install Rubygem (rubygems-0.9.5.zip or new version) from RubyForge (http://rubyforge.org/projects/rubygems/)
tract it and then run ruby setup.rb

4. install Rails

gem update
gem install rails –include-dependencies

The install could take minutes which will show you like this message,
Bulk updating Gem source index for: http://gems.rubyforge.org

If you encounter an error like this,

Updating installed gems…
ERROR: While executing gem … (Gem::RemoteFetcher::FetchError)
getaddrinfo: no address associated with hostname.(SocketError)

then you must make sure you have internet connection
or may be your are behind a firewall.

When you don’t have internet at home or at office, then you will need
to go to a netcafe and download some files separately from rubyforge gems
(http://rubyforge.vm.bytemark.co.uk/gems/)

The following files need to download,
a. actionmailer
b. actionpack
c. actionwebservice
d. activerecord
e. activesupport
f. rails

Next, install it one by one,
gem install activesupport
gem install activerecord
gem install actionwebservice
gem install actionpack
gem install actionmailer
gem install rails

5. create a rails application
i create it at C:\xampp\htdocs\FirstProject, so run
rails -d mysql C:/xampp/htdocs/FirstProject

6. open C:\xampp\apache\config\httpd.conf file in a text editor,
scroll to the bottom and add the following,

#################################
Listen 3000
LoadModule rewrite_module modules/mod_rewrite.so

ServerName rails
DocumentRoot “c:/xampp/htdocs/FirstProject/public”
<Directory "c:/xampp/htdocs/FirstProject/public/"

Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny
AddHandler cgi-script .cgi
AddHandler fastcgi-script .fcgi

#################################

7. start server with run this command,
cd c:/xampp/htdocs/FirstProject
ruby script\server

8. run the following at the command-line to keep from getting an “Unknown database” error,
rake db:create

10. make sure your connection to mysql can connect with right user and password,
set C:\xampp\htdocs\FirstProject\config\database.yml

11. to use Rails without a database, open C:\xampp\htdocs\FirstProject\config\environment.rb,
find the commented out line that reads,
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
remove the comment and you must remove the Active Record framework

Rails is a MVC (Model View Controller) framework, so all the output will happen in the controllers and views.

12. generate a controller named “hello” with an action “index”
ruby generate controller hello index

If you browsing to http://localhost:3000/hello, you will get
Hello#index
Find me in app/views/hello/index.html.erb

That message occurs because an action after “hello” doesn’t specified,
rails then search the “index” action and a view for this action doesn’t created too.

13. open the controller file at C:\xampp\htdocs\FirstProject\app\controllers\hello_controller.rb

Any public methods in a controller become actions in Rails. Notice the index action.
Add the following method just before end of file,

def world
@greeting = “hello world!”
end

The “@” sign lets views access the variable.

12. When browse to http://localhost:300/hello/world,
we have error like this,
Template is missing
Missing template hello/world.html.erb in view path C:/xampp/htdocs/FirstProject/app/views

13. “Template” is Rails word for the “View”. So Rails expecting the template at the path listed. Create or edit C:\xampp\htdocs\FirstProject\app\views\hello\world.rhtml, add the following text,

Try again, browse http://localhost:3000/hello/world and voila, “hello world, Ruby!”

Happy Rails developing and if you have any other learning resources that you’d like to share, please leave them in the comments!

Advertisements