Jump to content

Get Started With Ruby on Rails!

Recommended Posts

Ruby on Rails (RoR) is a revolutionary web programming language. Once I started to read about it, it became quite obvious why. The goal of Ruby is to make the code closer to spoken language and less of curly braces and semi colons. The Rails framework extends Ruby to expedite the task of building web applications. Unfortunately, installation is what discourages some people from learning a language. I had my own problems installing it since most of the resources I found were either written from Mac perspective or old Ruby versions. Now I'll share with you how I installed a Ruby on Rails development environment on my Windows computer.Definition ListHere are some definitions that you will help you to better understand terms used within these instructions.

  • Ruby - The base computer programming language from which the Rails framework is built.
  • Rails - The most widely used Ruby framework used for web development.
  • Gems - Ruby addons; like libraries in other languages.

Bundle Installation PackageIf you don't want to mess with installing the components individually, then you can just install the Rails Installer package. However, I'd recommend that you still read this post so that you can become familiar with Rake, Gems, etc.InstallationNOTE: I used Ruby Installer in this example. It comes with Ruby and Gems packaged together. Through Gems, you can easily install Rails. The alternative is to individually install Ruby and Gems, which is much harder.The first step is to download the package from RubyInstaller.org (also download the newest devkit, which is shown lower on the page). I chose the version at the top of the list (Ruby 1.9.2, at the time of this writing). Run the installer. Make sure to select both checkboxes (regarding the file extension and executables). Finish the installation. Ruby is now installed in your hard drive's root directory. Now, open the self-extracting devkit package. Extract into a convenient location. I chose the hard-drive's root directory, so it is shown beside the "Ruby192" folder. The Ruby Devkit makes gems build on Windows. Without it, Windows gems installation is a pain. To install Devkit, open up the folder and locate the file, "dk.rb". It should open up the command line briefly, and then shut down again. Now open of the Windows Command Prompt. If you can't find it, open "Run", type "cmd", and hit ENTER. To ensure Gems is installed, type gem -v, which should display the current version of gems. If it runs successfully, enter this code into the command line:

gem install rails

Hit enter, and wait. The blinker will move to the next line, and it may be a little while before anything happens. It is downloading and installing certain files, about 2MB. Depending on your computer's speed and the speed of your Internet connection, you should see a long list of "Installing ...." statements. Make sure you are connected to the Internet during this installation process. Your First Rails AppNow you are ready to create a Rails web application. Open Command Prompt (if it isn't already). The command syntax to create a new Rails project is "rails new {appName}". A sample command to create a project named "weblog" in the Ruby192 root directory would be this:

rails new "C:/Ruby192/weblog"

If this was successful, you would see a list of "create...". Note that if you had just executed the command "rails new weblog", it also would have worked, BUT it would have created it in the directory the command line is in (the path listed before the "greater than" symbol on the console). If you wish the change the directory in which the command line is working, you can simply execute "cd directory name". Let's now navigate to the directory in which we created our app. In my case, that would be cd "C:/Ruby192/weblog". If you were in the Ruby192 directory, you could execute "cd weblog", since it is an immediate child folder. Now that we are in the app's root directory, we can start the Rails server:

rails server

After you enter the rails server command, you should see a couple lines appear such as "Booting WEBrick". After it is fully started, you can navigate to "http://localhost:3000", and if all went well, you should see the Rails welcome page!Running Multiple WEBrick Servers at OnceIf you are creating multiple Rails projects simultaneously, then you will probably want to run more than one server at a time. If you try to boot a second server with the "rails server" command, it will respond with an error. The error occurs because you cannot have multiple servers running on the same port (usually 3000, or maybe 8808). To get around this complication, simply specify on which port you want to run the server like this:

rails server --port=port_number

You can then access your application at http://localhost:port_number.Creating DatabasesAlthough there's much more I could talk about, for now I'll just mention how to create the database for your rails app. To see what databases will be created, open the folder containing your rails app, open the folder "config", then open the file "database.yml". You can open it in your favorite code editor (see my signature for my favorite programs). This is a listing of the databases to create, and their specs. Rails encourages the use of three different environments for your application. Each environment has its own database. They are listed in database.yml:

# SQLite version 3.x#   gem install sqlite3development:  adapter: sqlite3  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: sqlite3  database: db/test.sqlite3  pool: 5  timeout: 5000production:  adapter: sqlite3  database: db/production.sqlite3  pool: 5  timeout: 5000

I'll go back to the command line now, and make sure I'm in the root directory of my app, in my case, cd "C:/Ruby192/weblog".Now we'll use a gem that was installed called "rake". You can see a listing of all valid commands by executing "rake -T". We are going to use the rake tool to create our database, as specified in "database.yml".

rake db:create

If the command is run successfully, it will respond with something like, "(in path)"Installation Complete!Now you're ready to getting rolling with Ruby on Rails. Buckle up and enjoy the ride!Here are some useful resources for learning Ruby on Rails:

Edited by Fmdpa

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...