Wednesday, April 17, 2013

Installing Rails on Windows

This post intends to provide an insight into installing Rails framework on a Windows PC. Dont bother about the OS version. For now, just try and follow the steps mentioned. For users who are not able to install it as mentioned below, try the "google-out-the-error" alogirthm. 

The first tool to download will be the editor. An editor is an application assisting us to write programs. 
Sublime text will be your editor for Rails apps for a long time. Install Sublime text from here  We would write programs in this editor and use the command line tools to run and debug the programs.

The next tool to download will be the browser. A browser is an application that runs the Rails apps. 
Although it is recommended to use all the browsers to test Rails apps, you may just use Chrome for the moment. Download chrome from here 

The next tool to download will be the entire Ruby as well as Rails kit. The following packages come included in this kit, aptly called Ruby-Installer kit:
  1. Ruby 1.9.3-p392
  2. Rails 3.2
  3. Bundler
  4. Git 1.8.1.2
  5. Sqlite
  6. TinyTDS
  7. SQL Server support
  8. DevKit 
Download the ruby-installer from here . After the installation finishes, allow the command prompt to popup with the configurations for Git. 
There can be issues here - for e.g. if you are using a PC with Git already installed, then you may have to change the username and email. Also you may have to copy the public key to your git account(if you dont have a git account, then create one here) Additionally you can learn about Git here, if you wish to.
  1. set username : git config --global user.name "Dimple Gusain"  
  2. set email: git config --global user.email "dimple.supi@gmail.com"
  3. Navigate to C:\Users\user\.ssh and open the id_rsa.pub file in notepad
  4. copy(Ctrl+A) all the contents
  5. Login to your account on Github
  6. Navigate to the Account settings page(access this menu from top right on the home page)
  7. click on SSH keys menu on the left menu
  8. click Add Key on the middle right
  9. paste the key and don't forget to give a name like "Dimple Gusain PC"

As for continuing the installation process, Read through the manual here and it should be fairly easy. The document instructs you to create a rails project titled "railsinstaller_demo" and lets you run it from the localhost. If you are able to see the "Welcome aboard" page, you are done installing rails on your machine. 

So far we have everything in place to start a rails project, and in fact the tools you just installed also helped you create a project. But we still need to edit the program in the editor.

Setting up Sublime Text editor:
  1. navigate to C:\Program Files (x86)\Sublime Text 2 (or C:\Program Files\Sublime Text 2 on some machines)
  2. locate the Sublime_Text_2.exe . Copy and paste it again in the same location. You should be able to see a file called Sublime_Text_2_copy.exe 
  3. Rename it to subl.exe
  4. Right-click on My computer and click Properties. 
  5. Click Advanced System Settings and click Advanced Tab. Click Environment variables button.
  6. In the "user variables for user" section, click edit.
  7. Add a semi-colon and the path of the exe file earlier. for e.g. blah;blah;C:\Program Files (x86)\Sublime Text 2 
  8. click ok  
Now close the command prompt and open it again(this restarts the command prompt and adds any new paths added). Type the following commands:
  1. type "cd.." as required to return to the root folder or the C:
  2. cd sites
  3. cd railsinstaller_demo
  4. subl . (this command invokes the sublime text editor with the contents of the current directory) This might take a few seconds to load depending on how fast your system is. Patience and peace.
  5. Now that you can see the project in the editor, you would want to the see the editor in action by editing something.
  6. Click on public folder and right click on the index.html to rename it to index_old.html
  7. Goto command prompt and create a new controller by typing "rails g controller welcome index"(this would create a couple of files, regarding which you neednt worry much now)
  8. Come back to the editor and on the left section(called navigator) locate the config/routes.erb
  9. uncomment the line root :to => 'welcome#index' and save the file(Ctrl+s)
  10. Also navigate to app/views/welcome/index.html.erb and edit the content between the <h1> tag to say "Dimple Gusain Rocks!". Save the file
  11. Go back to command prompt and type rails s
  12. Open the browser and type http://localhost:3000 to see the new edits
 What we just did:
  1. In Rails, every view has an associated controller. 
  2. So instead of creating a view(or a .html page), we create the controller(rails g controller welcome index) and Rails creates the view plus a couple of other files for us.
  3. Upon receiving a URL request, the default behavior of the rails framework is to look in the public folder for an image or file by that name. If not found, it will look in the routes.erb to locate the file. 
  4. We edited both the files to make sure the newly created welcome page is loaded when the root location is accessed. 
  5. We then started the rails server and accessed the application root to see the change


Committing the changes to Git and pushing the commits to Github
  1. in order to record the commits, you will need to install git in the current directory. git init
  2. add all the files to git git add .
  3. commit the changes git commit -m "Dimple Gusain's first commit"
  4. create a new repository in your account to hold a copy of the application online.
  5. log into your account on github. Create a new repository at the bottom right.
  6. name it "Dimple Supi" or just about any name that you like. Let the read me file be there too
  7. we will need to frequently push changes to the online copy of a project so we store the path in a variable for easy access.  git remote add origin git@github.com:supimon/Dimple Supi.git
  8. we then use this variable (origin) to push the changes to the online copy git push origin master
  9. in case of conflicts, type git push -f origin master to force update the changes(remember: with force update, all the changes on the online copy will be lost but since we are just starting this should be fine) 

A retrospective view:
  1. We set up the Rails programming environment by installing all the necessary tools.
  2. We set up an online repository to store a copy of the project online.
  3. We created and edited a few files.
  4. We initiated git in the project and committed the changes.
  5. We then pushed the commits online.
  6. Kool :)

That's all folks!