Ruby On Rails Tutorial 1 – Setting Up The Environment

5 minute read

 

Ruby on Rails is a web-app framework that is written in the Ruby programming language. Since its release in 2004, it has quickly gained popularity in the developer community. It aims to make developing web applications easier, efficient, and fun. In order to maximize developer efficiency, it is designed so that the best ways to do things are encouraged, and other ways discouraged. Plus, it takes less code to accomplish things than in alternative frameworks.

Similar to PHP, Rails code is run server side and can communicate with SQL databases.

Rails is open-source, and, as a result, is more stable, secure, and available. In addition, it’s completely free.

Some famous websites that run on Rails include Twitter, Hulu, Github, Groupon, Jango, and more. Some of these websites, like Twitter, have found that Rails doesn’t scale very well (when you are running a web-app with millons of visitors, Rails is either too slow or too limited). Because of this, they are considering moving to alternatives.

Installing On Windows

Install Ruby

Go to http://rubyforge.org/frs/?group_id=167 and download the .exe file for the latest version of Ruby. Run the installation wizard, and, on the second page of the installer, the page asking you what components you want to install, uncheck “SciTE” and check “Enable RubyGems”. Continue clicking through the defaults of the installer, and install anywhere you want.

Install Rails

To make sure you installed RubyGems correctly, open command prompt (Start Menu -> Run, and type “CMD”) and type in “gem”. You should see a fairly long message regarding Gems. If not, try running the installer again and make sure “RubyGems” is checked. If all went well, type in “gem install rails –include-dependencies”.

Install MySQL

Go to  http://www.mysql.com/downloads/mysql/ and choose “Microsoft Windows” in the “Select Platform” drop-down menu. Click the “.msi” download link for the version that corresponds with your version of Windows. Click the “No thanks, just take me to the downloads!” link near the bottom of the page, and click the download server nearest you. Run the installer, leaving everything default except for the “Modify Security Settings” page. Uncheck it. Next, download HeidiSQL. This will provide you with a GUI for your MySQL databases.

Download An IDE/Editor

When editing your project, you will want a text editor that is capable of understanding Ruby syntax. Having a file drawer within the app is very helpful.

There are a number of good Ruby IDE’s out there, some of the best being Intype, RubyMine (30-day free trial), Ruby in Steel (60-day trial), ZeusEdit,and E-Texteditor (30-day free trial). Try ’em all out and use the one you like the best. Intype and ZeusEdit are free, so use those if you don’t want to buy anything.

Installing On Macintosh

Install Ruby & Rails

Guess what? You’re already done! Ruby and Ruby on Rails come pre-installed on the Mac OS. Apple wants to make their computers more appealing to developers, and one way they do this is by pre-installing several programming languages onto them. Your current version may be slightly outdated by now, but it should do. Updating it can pose as a major and unnecessary hassle. Plus, Linux should be used if you are serious about running Rails on a server.

Install MySQL

Go to http://www.mysql.com/downloads/mysql/ and select “Mac OS X” in the “Select Platform” drop-down menu. Download a DMG archive that corresponds with your system.Once downloaded, open the DMG and run the .pkg that looks roughly like “mysql-5.1.58-osx10.60x86_64.pkg”. Next, double click the “MySQL.prefPane” in the DMG to install the System Preference window for MySQL that will allow you to start and stop your MySQL server. After you’ve done that, download Sequel Pro, which will provide you with a GUI for managing your databases.

Download An IDE/Editor

When editing your project, you will want a text editor that is capable of understanding Ruby syntax. Having a file drawer within the app is very helpful.

The best Ruby on Rails IDE out there is TextMate. It’s not free, but they offer a 30-day trial that you should definitely take advantage of. Once your trial runs out, you can try some hopelessly inferior alternatives like MacVim, Xcode, or Komodo Edit.

Running The Server

Now that we’ve downloaded and installed all of the applications and libraries we need, we can create a default Rails app and view it in the browser.

On Windows

First, we need to create our app. To do this, open up command prompt (Start Menu -> Run, and type “CMD”) and navigate to the directory on your computer where you want your projects to install. Use the commands “dir” to see the contents of the directory you are currently in and “cd” followed by the name of a folder in your current directory to navigate to that folder. I suggest simply making a folder named “rails” on the first level of your local drive and typing “cd rails”. When you are in your desired folder, type “rails helloworld”. This will create the helloworld project folder in your current directory.

While in the helloworld directory, type “ruby script/server” to start the Rails server. Go to http://localhost:3000 in your web browser, and you should see the Rails “Welcome Aboard” page. Congratulations! You’re all done setting up your Rails environment and are ready to start coding!

On Macintosh

First, we need to create our app. To do this, open Terminal and navigate to a desired location on your system using “ls” to list all of the folders and files in your current directory, and “cd” followed by a folder name to navigate into a folder. I suggest simply typing “mkdir rails” as soon as you open terminal to make a folder called “rails” in your home directory (and then type “cd rails” to navigate into it). When you are in the folder where you want to create your project folder, type “rails helloworld” to make a folder with all of the default project files in it. When creating a project, you use the “rails projectname” command (replacing “projectname” with your project name).

Next, navigate to that folder in terminal. If you haven’t closed your Terminal window, then you should be able to get to your project folder by typing “cd helloworld”.

Now we can actually start the server. Type “script/server”.

If all went well, you should now see a “Welcome Aboard” page at http://localhost:3000 . Congratulations! You’re all done setting up your Rails environment and are ready to start coding!

Leave a Comment