Tools of the Trade
There is a pre-packaged Ruby on Rails installation called “Instant Rails”, which you can download from here. This will install a whole bunch of tools into a single folder on your PC, including Ruby, Apache, MySQL, PHP and PHPMyAdmin. What’s especially cool about this installer is everything runs from inside a single folder, and everything is preconfigured, so you don’t need to tweak any text files to get things going! Sweet.. Just unzip the ZIP file that you downloaded (above) so that it’s all contained within “c:\instantrails”, easy as that..
If you have time to kill the rest of this section outlines how you could set everything up manually, if you are going to use InstantRails just skip ahead to the next section…
To install everything manually (just for fun), first I downloaded the ruby “One-Click Installer for Windows” from here. Run the installer and accept all the default settings. This will install Ruby on your PC.
The One-Click Installer (see above) will also install the RubyGems package manager. You will use RubyGems to download and install Rails. To do this, simply open the command line (yeah baby!), and type: “gem install rails –remote”, then press Enter. You will be asked a couple of questions, to which you simply enter “y” (for “Yes”), and press Enter. Cool. Alternatively, if you don’t like the command line approach, you can download Rails in ZIP format from here. (Tip: In the future, as new versions of Ruby are released, simply repeat this step from the command line to download and install the latest version!).
Rails supports a bunch of different databases, most people (including us) seem to use MySQL. Download the latest version from here – look for the section on the webpage called “Windows downloads”, then look for the download called “Windows Essentials (x86)” and download and run the installer (exe). Accept all the default settings.
We now need a MySQL graphical interface. We don’t really need one – we could just use the command line – but we’re going to go ahead and install MySQL-Front, which you can download from here. This tool costs $35, but you can install a 30-day trial version. Download and run the installer, accepting all the installer defaults.
Hello World
From here on I will assume you that you are using InstantRails and that it is installed in “c:\instantrails”. If you installed everything manually (see above) you will need to tweak settings as you go…
Below is a quick summary showing how to get an application up and running. There are more comprehensive tutorials on the web that explain the steps in more detail if the following steps do not make sense to you:
- To start InstantRails run “c:\instantrails\instantrails.exe”. If it asks whether you want to regenerate configuration files, click “OK”. This will start the InstantRails server – you will see an icon in your system tray. ( You can also start the InstantRails server from the command line by typing “ruby script\server”, or you can stop the server from the server console window by pressing CTRL+C ).
- We are going to create a new application called “TestApp”. Open your InstantRails server (icon in the system tray), and select “Rails Applications” -> “Manage Rails Applications”. Then click on “Create New Rails App”, this will open a console window. Now type “rails TestApp”, and press Enter. That will create a subdirectory called “TestApp” with all the basic files you will need to get started. Have a look in “C:\instantrails\rails_apps\TestApp” to see what’s there.
- In the InstantRails window that you were using above, click “Refresh List” – you should now see “TestApp” listed under “Rails Applications”. Tick it, then click on “Start with WEBrick”. WEBrick is a web-server that is part of InstantRails. Starting WEBrick here will let you browse your new application locally so that you can test it.
- Next we are going to create the database for this application. I will use MySQL. First I create a new database called “testdb”. In testdb I will create a table called “People”. It is important to note that your table names need to be in the plural form, as Rails will use that as part of its functionality later on. So tables names should be in the form of “people”, “orders”, “sales” etc. My People table just consists of a PeopleID (int, autoincrement), FirstName (varchar) and LastName (varchar) field.
- Now we need to hook our Rails app up to the backend. We do this using the “C:\instantrails\rails_apps\TestApp\config\database.yml” file, which I will modify (using WordPad) as follows:
adapter: mysql
database: testdb
username: Gary
password: MyPassword
host: localhost
You will notice that in that file there are settings for three tables, “Development”, “Test” and “Production”. Use the same settings for all three (more on this later on). - Now we are going to create Controllers and Models for the App. I will explain this later on, but for now just type:
ruby script/generate controller person
[ENTER]
ruby script/generate model person
[ENTER]
Those two commands will cause Rails to create a whole bunch of files for you (“Controllers”, and “Models”), which we will delve into later on. Note that our table is named “People” and in the commands above we used “Person”. - We are now going to edit the Controller file that was created above. Open “C:\instantrails\rails_apps\TestApp\app\controllers\person_controller.rb” and edit it as follows:
class PersonController < ApplicationController
scaffold : person
end
Then save it. (Note: There is no space between the “:” and the “p” in “person” above. ) - Because you have changed this file you will need to restart the WEBrick webserver that you started earlier on.
- Now browse http://127.0.0.1:3000/person
Lessons, Part 1
Inside a Ruby application you will use the following type of file (stored in subfolders within the application folder): The “Controllers” subdirectory stores controller classes. These handle web requests from the user. The “Views” subdirectory holds the display templates, which pretty much manage what the user sees. The “Models” subdirectory holds classes that wrap up the data stored in the database. The “Helpers” subdirectory holds any helper classes, which are used to assist the other three types of files (classes) already mentioned above.
To “point” a Ruby application at a database you need to edit the application’s “database.yml” file, using WordPad (WordPad, not NotePad). This file is found in the application’s “Config” subfolder.
You create a model class as follows: “ruby script\generate model Product”. That would create a class called “Product”, which would wrap up the data for the “Products” table in our database. Ruby is smart enough so that if you have a table called “People” and create a model class called “Person”, it will auto-magically set up the “Person” class to reference the “People” table!! The naming convention is simply that your table must use a plural name, and your model class must use the singular equivalent, so recipe maps to recipes, company to companies, person to people, etc. Each model class gets generated to include methods to handle accessing the data within the mapped table that the class wraps. Cool.
To create a controller class, simply type: “ruby script\generate controller Product”. The controller handles web requests from the user. You will use the controllers to build your actions and views of the data. To start with though, if you simply enter “scaffold :product” inside the ProductController class, you will have all the basic features (Edit, Delete, Insert, View, etc) done auto-magically! Then you can simply replace the automatic ones with your own, when you have time to do so.
Wow
My first major “wow” moment came pretty soon in this learning curve. I created a table in a MySQL database with three fields in it. I then used the command line functions mentioned above to generate the Model classes, and the Controller classes. I then browsed my new website, and had a fully working website, which interacted with the table in my database! Cool. The real “wow” moment came though when I added two new fields into that same table in the database, then simply hit the “Refresh” button on my web browser, and the new fields appeared on my website! Just like that! Nice…!
Lessons, Part 2
When Rails generates files for you, the actual web pages that the user sees are not pretty. You need to make them pretty yourself. When you run the generate Controller script (from the command line), subdirectories are automatically created for you inside your “app\views” subfolder.
Basically when a user requests a web page, the Controller class will handle the request. If you are using the “built in” “scaffold” feature we mentioned earlier inside your controller class, then the magic sort of stops there. If you have implemented your own controller method inside that class, then your method will be used. Once the controller class has done its thing, it will look for a template to render and return data to the client’s browser. These templates are located in your “app\views” subfolder, and are mostly standard HTML, with some embedded Ruby code to “add” the data from the database into the HTML that will get returned to the user.
Just to repeat from above.. For every table in your database that you want to integrate into your website, you will need to generate a Controller class, and a Model class, as explained above.
Use the Model classes (in your “app\models” subdirectory) to set up relationships between the various tables. Do this using the “belongs_to” and “has_many” commands.
A fairly new feature in Ruby is that you can now type (from the command line) “ruby script\generate scaffold Product”, and it will generate both the Model and the Controller, plus it will create all the scaffold code and view templates for all basic operations! You can then edit these template-generated functions directly! Be careful using this though because it will replace any existing functions that you may have written..
You can use the “layout” command within your Controller class files to “link in” a layout file (which is basically an html file, with a tag representing where the generic data should go). This lets you easily remove the formatting from the various Controller classes. Pretty much like a CSS file, just using Html.
Last cool tip for the night.. If you type “rake stats” from the command line Rails returns a bunch of useful information about your app, including the number of lines of code you have written in the various classes.
Going Live!
I haven’t done this yet, but I will more than likely be hosting my first Rails app with Dreamhost, and found a good article at http://www.railshosting.org/#dreamhost, which I have summarized here:
- Firstly, you will need to connect to your webserver via SSH, so download and install an SSH client. I use Putty.
- Set up your domain using the DreamHost control panel, and make sure that “FastCGI Support” is ticked. I installed my Rails app into the root folder of my domain, I haven’t tried using sub-domains or sub-folders yet.
- Use Putty to connect to your webserver via SSH. Type “mydomain.com” for the “Host Name” setting, and ensure that “SSH” is ticked as the connection protocol, then click “Open”. A console window will appear. Type in your user name and password, then type the following to create your Rails APP file structure on your webserver.
cd mydomain.com
[ENTER]
rails MyApp
[ENTER]
exit - Close your SSH client.
- If you now FTP into your webserver you will see that there is a new folder in your domains root folder called “MyApp”, which will match the Rails folder you created locally earlier on.
- In your local app folder, you need to create copies of database.yml and environment.rb, as these files will be different on your local installation and on your web (live) installation. I created copies called “database.yml.online” and “environment.rb.online”. Now you can edit these four files locally, then upload them to your server and simply remove the “.online” portions of the filenames to override the files on the webserver. In “database.yml.online” you need to change the “production” sql server details to connect to your live sql backend. In “environment.rb.online” you need to uncomment the following line:
ENV['RAILS_ENV'] ||= ‘production’ - You also need to edit and upload the “.htaccess” file in your “public” folder, by adding a “f” after the “.” in the following line:
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] - Now you can develop your Rails app locally, and then simply upload your entire “MyApp\APP” folder (NOT the other folders inside “MyApp”) over the “MyApp\APP” folder on your webserver.
- You should now be able to browse to http://www.mydomain.com/myapp/public/orders


Recent Comments