So I've been playing around with Ruby on Rails recently, well just today, anyway I wanted to see how easy it was to get a development environment set up, write a sample application and deploy this to the web somewhere.
This post is a step by step of the pain I went through getting this working on my Win 7 machine, and yes I know I'd be better doing this on Unix or a Mac, and I know Win 10 is just around the corner (although I suspect a lot of this will remain the same, it is still Windows!) but my current dev machine is rocking Win 7 so here we go;
- A Win 7 machine with admin access
- Git installed (msysgit) and working knowledge of this and the bash shell
- A bitbucket account and experience working with these repos (or github, wevs)
- Google-Fu > white belt, just in case!
First of all un-install and delete any previous ruby installs you may have lying around, I had 1.9, 2 and 2.2 for some reason - so let's lose those as we want a clean slate to start from.
Next, from stack overflow this article will get you ruby 1.9.3 and rails installed and working, I know it's not the latest version but I spent an evening Googling this to death and found that these steps worked for me so I'll start here with 1.9.3 and once happy with this I can always upgrade;
- Download Ruby 1.9.3 from rubyinstaller.org
- Download DevKit file from rubyinstaller.org - for Ruby 1.9.3 use DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe
- Extract DevKit to path C:\Ruby193\DevKit
- Run the following commands;
- $ cd C:\Ruby193\DevKit
- $ ruby dk.rb init
- $ ruby dk.rb review
- $ ruby dk.rb install
Now you should be able to just new up a rails application, but I found that I had missing gem dependencies, starting with JSON.
To resolve this, you should be able to install JSON
- $ gem install json --platform=ruby
Ok so now I should be able to new up a rails project, for example;
$ rails new C:\dev\rails-example\rails-example
Mining for Gems
In reality I found I was still missing some gems, and successive attempts at creating a ruby project using the command above yielded the following suggestions, (1 at a time I might add, thanks ruby!);
- gem install rake -v '10.4.2'
- gem install mini_portile -v '0.6.2'
- gem install nokogiri -v '188.8.131.52'
- gem install debug_inspector -v '0.0.2'
- gem install binding_of_caller -v '0.7.2'
- gem install coffee-script-source -v '184.108.40.206'
- gem install execjs -v '2.5.2'
- gem install coffee-script -v '2.4.1'
- gem install coffee-rails -v '4.1.0'
- gem install columnize -v '0.9.0'
- gem install debugger-linecache -v '1.2.0'
- gem install debugger-ruby_core_source -v '1.3.8'
- gem install debugger -v '1.6.8'
- gem install multi_json -v '1.11.2'
- gem install jbuilder -v '2.3.1'
- gem install jquery-rails -v '4.0.4'
- gem install rdoc -v '4.2.0'
- gem install sass -v '3.4.16'
- gem install tilt -v '1.4.1'
- gem install sass-rails -v '5.0.3'
- gem install sdoc -v '0.4.1'
- gem install sqlite3 -v '1.3.10'
- gem install turbolinks -v '2.5.3'
- gem install tzinfo-data -v '1.2015.5'
- gem install uglifier -v '2.7.1'
- gem install web-console -v '2.2.1'
There has to be a better way to resolve this, and there probably is it's just that I haven't figured it out yet, this could be due to me using an older version of ruby, or I need to run another bundle command such as update? If and when I figure it out I'll update this post.
Start her up
Anyway, now we've resolved that, next we can start her up with the following command;
$ rails server
This boots the app and starts it on a specific port of localhost (3000 by default) so we can browse the out of the box default Rails site, finally!
Well I guess I'd better find a tutorial to help me understand Rails a little, luckily there are loads of tuts on the web, I found this one https://www.railstutorial.org/book
Following this tutorial I started a new rails app called hello_app
- $ mkdir C:\dev\hello_app\
- $ cd C:\dev\hello_app
- $ rails new hello_app
This creates the rails app in a folder called hello_app, inside the folder I already created called hello_app, I suspect this may cause me an issue later but whatevs let's roll with this for now.
Next we need an IDE, I chose Aptana Studio 3 as its free and I'm a cheapskate; get it here: http://www.aptana.com/
Once you open this up set your dev folder (in my case C:\dev) as the workspace, then navigate to your app folder and promote it to a project (from the right-click menu in the project explorer window of the IDE).
It looks like this creates you a .project file in your app folder, which presumably stores user/app settings etc, it also changes the icon on the app folder in the project explorer window of Aptana - fair enough, we'll check in this .project file to our repo as well then
Oh yeah, I set up a new repository in github for this, just to keep track of what I'm doing, you can see the repository here if you're interested;
The tutorial I followed recommended using Heroku to deploy my app too, to do this I first updated the Gemfile to include PostgreSQL (as Heroku uses this db), and this meant more gem dependency faffage.
This time I was using the terminal window in Aptana, and between each gem install running bundle install to pick up the new gems and reinstall dependencies, which is what the 'rails new' command runs anyway, so the same issue/resolution but in my IDE within the sample project now, nicer but still rubbish;
- gem install pg -v '0.17.1'
- gem install rails_serve_static_assets -v '0.0.4'
- gem install rails_stdout_logging -v '0.0.3'
- gem install rails_12factor -v '0.0.2'
New gems done, phew!
I could/should have used the command 'bundle install --without production' to prevent the need to install locally (but still update the lock file used on deploy to production), however I've done it now so have PostgreSQL and dependent gems installed locally and an updated lockfile ready for deploy to heroku (according to the tutorial, we'll see whappens on deploy!)
I've used Heroku before, but from my ubuntu machine, so we need the toolbelt installing here (my Win 7 machine) first, you can get that from here;
You may also need/want to update your version of git for windows (msysgit), Heroku toolbelt informed me my version was riddled with security issues (1.9.3) so I upgraded to the latest (1.9.5 at time of writing this) by getting it from here; https://msysgit.github.io/
So run these commands (and add your info when prompted) to login, create and deploy to heroku:
$ heroku login
$ heroku create
$ git push heroku master
FYI what the 'heroku create' command does is add a second remote to your repository, which is heroku now instead of bitbucket (or Github if you're rolling with that). Deploying then is just a matter of pushing to the new 'heroku' remote, nice!
Heroku push rejected, WTF?
If you get any hassle from the push to Heroku then you probably haven't got your app in the root of your repository, which is the issue I found. This was due to my unneccesary nested folder structure that I mentioned earlier, yep that bit me. So ensure your rails app is at the root of your repository.
The error I got was;
Heroku push rejected, no Cedar-supported app detected
Which is odd as the command '$ heroku create' created me a Cedar app (it detects this from your Gemfile I believe).
This stack overflow article helped me, specifically the 3rd answer about the rails app not being in the repo root;
Re-issuing the push to heroku now failed for a different reason, mainly that sqlite is not supported by heroku (they use postgres as mentioned earlier), so I had to do some re-jigging of my Gemfile to bring in line with the tutorial, the key part being that I moved the gem sqlite into the development and test group so it is only required in dev and test and not production. I also added the spring gem as the tutorial suggested I'd need this, as expected this resulted in yet more gem dependency chasing, as per below;
- gem install slop -v '3.6.0'
- gem install spring -v '1.1.3'
Ok so after moving my rails app up to the root of the repository, and after sorting out my Gemfile and chasing gems I was able to push to my heroku remote and deploy my sample rails app...
Except no, my defaut .gitignore file ignored my config/secrets.yml file - and this is needed by heroku, so I commented out that line in .gitignore, committed and re-pushed to heroku....
My new hello world rails app is up and running, you can see this magnificent beast here;
For a real world app you probably don't want to retain your config/secrets.yml in a public repository - to get around this you could probably create a secondary private repo with the same heroku remote and contain the config/secrets.yml here, but then you'd have to pull in the latest from the public repo and merge this before pushing form the private repo to heroku, so a bit of faffage there.
If anyone has experience of doing this please let me know in the comments if a) my suggestion above would work and b) you have a better solution to this. Cheers all.
So what have we achieved then?
- Installation of Ruby and Rails on Windows 7
- Setup of IDE
- Sample Rails application created
- Source control implemented
- Simple deployment process set up and proven
- Free basic hosting in Heroku for a Rails application
Not bad for an evenings work eh? Right that's enough blog for now, next I need to add some functionality to my shiny new rails app, catch you later folks.