Zero to Web App in 15 Minutes with Heroku and Ruby on Rails 3
If you are interested in rapid mobile application development, Ruby on Rails, Sencha, Heroku and APIs are some of the buzz words you may have heard or are actively using. This blog will kick off a multi part blog series where I will take you from zero to mobile app in a very short time. This first blog will focus on getting your first web app up and running in under 15 minutes and the kicker is that this will all be achieved on a PC.
From here, I will integrate a mobile friendly framework like Sencha for the mobile web component, and then create a mobile app with Red Foundry, appMobi, Titanium, Ansca, and PhoneGap. Time permitting, I will also create a native WP7, iOS and Android mobile app so that you can step through the process with me. For those of you out there that are Mac lovers, I do have a Macbook Air and will be posting adendums to these blogs that are Mac specific.
So why this blog and why now? I attend numerous technical meetups in the Seattle area that include TechStars, Startup Weekend and Lean Startups. I see lots of non-technical founders struggling to find technical co-founders as well as struggling to get a grasp the various technologies that bleeding edge startups are using. I hope that these blogs can serve as a guide in the ever growing pile of “latest and greatest technologies” as these new and shiny toys seem to come out faster and faster every year. It was only last year that Ruby was the darling of the startup world and now it seems that Node.JS maybe the new shiny toy.
In trying to get to a functioning mobile app as fast as possible, I have found the traditional “setup your own server” and “roll your own code” somewhat more time consuming than first anticipated. Not to mention that the majority of Ruby tutorial seem to be written for Mac. Well, I decided to try again this last week and boy have the tools come a long way as far as development on a Windows 7 machine is concerned.
By spinning up a cloud server, you don’t have to setup a server in your living room and get the scripts right. You don’t need to add extra memory to your system, make sure there aren’t any conflicting drivers and that your kids don’t trip over the bundles of wire criscrossing the room.
From the beach, you can upload your code and hit the new temporary server to see your app running. Now how beautiful is that? Don’t reinvent the wheel, just get your app up and running in the fastest way possible with a solution that can scale when you throw money at it.
If you feel that you can get your nginx/mongoDB server setup running, hooked into dyndns.com flawlessly and accomplish this in under two weeks, then by all means, ignore this paragraph. However, if you are like most developers that don’t have sufficient experience with bleeding edge web servers, then consider using a cloud server solution where the servers are ready to go and you don’t have to fiddle with configuration files.
So to get started with developing a cloud hosted Ruby app with your PC, download/install the following software packages:
- Install “Git” — http://code.google.com/p/msysgit/downloads/list?can=3 – Look for the link entitled “Git-1.7.6-preview20110708.exe”
- Create “SSH Keys” — I had a heck of a time here trying to find the hidden “.ssh” folder on my PC. I’ll update this part later.
- Install “Ruby” — http://rubyinstaller.org/ – Just click on the big red “download” button
- Install “Gem” — http://rubyforge.org/frs/?group_id=126 – Look to the top of the page for a package entitled “rubygems-1.8.6.zip”
- Install “Heroku Gem” — Click on Start>All Programs>Ruby 1.9.2-p180> gem install heruku
- Update SqLite DLL – http://www.sqlite.org/sqlitedll-3_6_23_1.zip – download the zipfile and unzip this file into your “c:ruby192bin” directory
Step 1 – Create a Base Ruby App
Once you are finished installing the software, start the Ruby Command Prompt program (located at Start>All Programs>Ruby 1.9.2-p180). At the command prompt type the following commands:
- mkdir app
- cd app
- rails new hello (this tells “rails” to create a new app template in a directory called “hello”)
- cd hello (get into that directory called “hello” that you just created)
- bundle install (update the app dependencies to make sure there are no conflicts)
- rails generate controller hello
Step 2 – Create a Ruby Route
Open the file config/routes.rb. Almost at the bottom (line #57) is this line:
- # match ‘:controller(/:action(/:id(.:format)))’
Remove the # in front so the line looks like:
- match class=”string”>’:controller(/:action(/:id(.:format)))’
Safe the routes.rb file.
Step 3 – Create a File For that Route to Serve Up
Open up Notepad (I personally use notepad ++). In a blank file, type in the text “hellow world” or whatever you would like to have show up when a user visits your site.
Finally, save this file under the name “index.html.erb” in this directory: “app/views/hello”
Step 4 – Upload Your Code To the Cloud!
Push your code from your computer to GitHub:
- git init
- git add .
- git commit -m “new hello world app”
Step 5 – Fire Up Your Remote Web Server!
Here, you need to transfer your code from Github to a Heroku so that you can see if your code actually works or not.
- heroku create sushimaru3
- git push heroku master
- heroku rake db:migrate
Step 6 – Test Your App!
After you type “git push heroku master,” the command line will prompt you that the transfer was successful and list out the name of the server that you just created. The webserver name should be something like this “http://evening-leaf-558.heroku.com/“
If you go to that web server address, you should see this:
Now, go to your server-name/hello (I went to this address: “http://evening-leaf-558.heroku.com/hello“) and you should see the route that you created and your “hello world” statement (something similar to this):
Common Pit Falls
Error with “git push heroku master” command
Reference this article (http://devcenter.heroku.com/articles/keys) to see how to flush your RSA keys and upload your current RSA keys to Heroku. Odds are this should solve your problem.
I can’t find my .ssh file?!
Read the output of the “ssh-keygen -t rsa -C “firstname.lastname@example.org“” function because it actually tells you where the RSA files were placed.
Here are the articles that I referenced many times to obtain the base knowledge to write this article:
Rails Reference: http://mentalized.net/journal/2010/02/05/hello_rails_3_world/
Heroku Reference: http://devcenter.heroku.com/articles/quickstart
Git Reference: http://help.github.com/win-set-up-git/Heroku
Key Issue: http://devcenter.heroku.com/articles/keys?
Sqlite DLL Issue: http://www.ruby-forum.com/topic/149431