Beginners Guide: Setting up Ruby on Rails in Windows
Posted by Tom Rossi on July 24, 2006Comments (18)
Ruby on Rails, Tutorials
Beginners Guide:
1. Getting Ruby on Rails Running on Windows
2. Introduction to the MVC architecture.
3. Starting With The Model
Setting Up Ruby on Rails on Windows
I'm going to be posting up some tutorials to help recovering Microsoft developers learn about using Ruby on Rails (RoR) for developing web applications. The tutorials will be really basic in the hope that I can help people avoid some of the gaps I found while trying to learn this stuff.
What is Ruby on Rails?
Ruby on Rails refers to the ruby programming language running in a rails framework. If this were the GRE, I would say Ruby is to Rails as C#.NET is to .NET (who are we kidding, the GRE only cares if you know the meaning of conflagration). Ruby is a young object-oriented programming language that was released in 1995 by Yukihiro "Matz" Matsumoto. Matz created a programming language that is intuitive and honestly fun to program with. Ruby gained popularity and had a following long before the release of Ruby on Rails about 10 years later by the infamous DHH - David Heinemeier Hansson (if you don't know he is infamous yet, trust me. You will.) DHH took the Ruby language and implemented the Ruby on Rails framework at 37signals with the Basecamp project. He generously made this available via open-source and the web community has been eating it up ever since.
Additional links: http://en.wikipedia.org/wiki/Ruby_on_rails http://en.wikipedia.org/wiki/Ruby_programming_languageWhat's the big deal?
Undoubtedly, there is plenty of hype surrounding RoR. Let me speak specifically for myself. RoR provides a super streamlined environment for developing web-based applications that are driven from SQL databases. I am fortunate that my experience has only been with developing new applications that do not require legacy databases or other enterprise interactions. So when I tell you that my development time is easily 3-4 times faster than developing in an ASP.NET framework, you have to take that into account.
Don't Repeat Yourself - DRYOne of the things that becomes apparent as you develop in RoR as the idea that you shouldn't have to repeat yourself. If you find that you are repeating yourself, most likely you are doing something wrong. For example, if you have already written your validation rule for an email address provided for a user profile, you shouldn't have to repeat that validation on each form that requests it and you also shouldn't have to put that validation into your database code. DRY, like some of the other RoR philosophies is something you have to kind-of ease into. At first, you think its just nuts. After you've embraced it, you can really soar through development.
Convention over ConfigurationTo a certain degree, this is an extension of the philosophy of embracing constraints. In other words, by following conventions and kind-of boxing yourself in, you can save a lot of time. Within RoR you are constantly encouraged to embrace the conventions to save yourself time with excessive configuration. We'll be seeing a lot of examples of this as we go through the tutorial, but for now let me just give one example: table names. When we get into the modeling of our data, you will see that Rails encourages you to name your tables after your classes in a plural form (e.g. the class of 'User' would expect to have a table 'users'). Already, some of you are moving your mouse to close this down and get back into Microsoft Visual Studio and SQL Server 2005 and try to forget that you even began reading this blog. Don't do it! I hear you! This seems weird! Why would you have to name your table like that? Shouldn't I be able to name my table 'tblUsers'. Well you can name your table whatever you want but you'll need to spend time configuring it. If on the other hand, you embrace this convention, you can save yourself a lot of time in configuration code, documentation, and repeating yourself. If you've named your class 'User', your table is 'users'. If I see a table named 'vehicles' I know it is refers to a class 'Vehicle'. Convention over configuration and the embracing of constraints -- it does the body good.
Enough already, lets get this running!
Okay, so now I am going to walk through getting your own RoR environment set up in a Windows XP or Windows Server environment. We haven't talked much about coding yet, but I think it is a good idea to have the software installed so as I get time to post these tutorials, you can follow along and play around. Please post questions if I skip any steps, I really want this to be a tutorial for win-dummies.
1. Installing Ruby
The first thing we need to do is install Ruby. As I mentioned before this is the beautiful language that will be used for all of the logic we will be writing in our application. So first you'll need to download the Windows Installer for Ruby. Please make sure you check for the latest version.
I uncheck everything except for the Base Install, Documentation, and RubyGems Package Manager. Go ahead and install it right off your root, or if you want to hold on to MCP certification, you can install it in C:\program files\ruby. One thing I promise is that none of the things we will be installing are bloated applications that will require a clean install to remove from your system -- trust me, I'm anal too (man I hope that doesn't get me in trouble in the search engines). This is something you will see if you are new to the Open Source community and the dark underworld of Linux: most applications are easily installed AND uninstalled.
2. Installing Rails
Okay, so if you are a MS win-dummy like I was, this is gonna blow your mind. Along with Ruby, you installed something called RubyGems. This is something crazy. Its called a package manager and allows you to just type in a command at the prompt and have it automatically download software off the Internet and install it. I can hear you Linux users snickering! Be nice, this is new to some of us! Basically, you now have the ability to download and install what are called GEMs off of the Internet. So people that write open source code can release it in the form of a GEM and then you can simply type in a command and have it downloaded and installed. So, to install the GEM package for Rails, just go to the command line and type:
gem install rails --include-dependencies
It doesn't matter what directory you are in when you type this, I believe it will always install the software in the ruby\lib\ruby\gems folder. Also, it may take a few minutes to complete. Rremember that it has to download first, then install.
3. Creating A Rails Application Framework
Okay, now that we have Ruby and Rails installed, we can prepare to serve up our first web page. Rails uses Generators to simplify the creation of simple code. Generators often install just the minimum code necessary to get you started. You are usually expected to edit this code to have it do what you want. At this point, I remember not wanting to use generators because I wanted to understand the code that was being created. Don't worry and hang with me. As we go through the future tutorials we will break down what the generators are doing. For right now, we just want to make sure we have a Rails application framework to test out our installations. So you will want to get into the directory you would like to keep your rails applications. The MS in me has me using the My Documents\My Rails Projects folder, but you can place it wherever you want. Head back to the command line and type in:
rails my_new_app
This shows you all of the files that have been created to make the framework for your application. Almost there! So close!
4. Serving Up Your First Page
Okay, so we've got our framework all set so you just go to the command prompt in your application folder and enter the command:
ruby script/server
You've done it! Now you have a little web server running on your system using port 3000 and serving up your new rails application. Bring up your browser and go to http://localhost:3000 and you should see this shiny welcome message:
Conclusion
Okay, so hopefully this has been enough to wet your appetite and get you playing. I need to get back to my work on Tick so I need to leave you here for now. In our next tutorial we'll be looking at the Model View Controller (MVC) approach to web applications and getting started building our first application using our newly installed environment.
Please leave comments if this has been helpful so I can get motivated to keep the series going! Also, feel free to point out anything you think I may have missed or misspoken.
Give us a piece of your mind.
18 comments thus far - add yours