In this tutorial we will go from zero to ninja speed in approximately ten minutes. If you have problems approaching ninja speed, it is probably because you are in ruby version management hell, gem dependency hell, or any other number of hells that brave would-be ninjas are occasionally trapped in. Should you end up here, prayer and Google may help you.
Step one: Create your rails app.
rails new ninjaspeed
Step two: Add the database.com and helper gem to your GEMFILE (this will be in the ninjaspeed directory)
gem 'databasedotcom' gem 'databasedotcom-rails'
Also add the postgres gem (you aren’t using the db on Heroku but need it anyways) and a gem to handle precompiled javascript (for the JQuery Mobile):
gem 'pg' gem 'therubyracer'
Step three: Install the new gems
sudo bundle install
Step four: Add scaffolding for a new resource that corresponds to a resource in Salesforce
rails generate scaffold_controller Lead FirstName:String LastName:String Company:String
Add an entry in your routes.rb file
resources :leads
Rails is straight up MVC, and you just created a controller and a view. Check out your app/controllers and app/views folders to see them. Salesforce/Database.com will be your Model.
Step five: Deploy this baby to Heroku
git init git add . git commit -m "i love databasedotcom" heroku create git push heroku master heroku addons add:piggyback_ssl
Step six: Take the new funky fantastic name you got and enable remote access in Salesforce
Login to salesforce. Go to Setup -> Develop -> Remote Access -> New Enter the address of your app (e.g. https://stormin'-samurai-558.herokuapp.com) You will get a client key (this is also the client_id) and your client secret
Step seven: Add your salesforce credentials to a file in the config folder called databasedotcom.yml
databasedotcom.yml #### client_id: 3MVG..... client_secret: 1323224123... username: mysalesforceusername@login.com password: passwordPlusSecToken host: login.salesforce.com <-- use test.salesforcecom if using a sandbox debugging: true
Step eight: Connect your app to Salesforce
Add this to your app/controllers/leads_controller
LeadsController < ApplicationController include Databasedotcom::Rails::Controller
In the same file you need to add a few more required fields. Navigate to the create method and add three lines:
def create
@lead = Lead.new(params[:lead])
# ADD ME
@lead['OwnerId'] = '005U0000000IekIIAS' <-- ownerId here is the Id of the User you want the Lead associated with
@lead['IsConverted'] = false
@lead['IsUnreadByOwner'] = true
# END
Thanks to some backend magic, the databasedotcom-rails gem automatically initializes the client you need to connect to salesforce so you don’t have to worry about it.
Step eight: Add in a jQuery mobile stylesheet so this doesn’t look super ghetto (optional)
Navigate to app/views/layouts/application.html.erb and edit these lines to the header:
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.css" /> <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> <script src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script>
Put the yield tag in the body in a div like so:
<div data-role="page"> <%= yield %> </div>
For your apps/views/leads/new.html.erb, divide into a couple of sections
<div data-role="header"> Lead form </div> <div data-role="content"> <%= render 'form' %> </div> <div data-role="footer"> If you want it </div>
Navigate to config/environments/production.rb, find the config.assets.compile, and set it to true (turn on the live compiliation of assets)
config.assets.compile = true
Step nine: Remove extra stuff from your _form.html.erb/strong
This is all you need:
<%= form_for(@lead) do |f| %>
<div class="field">
<%= f.label :FirstName %><br />
<%= f.text_field :FirstName %>
</div>
<div class="field">
<%= f.label :LastName %><br />
<%= f.text_field :LastName %>
</div>
<div class="field">
<%= f.label :Company %><br />
<%= f.text_field :Company %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Step ten: Deploy to Heroku
git add . git commit -m "I love databasedotcom even more now" git push heroku master
Navigate to yourapp.heroku.com/lead/new, and presto-chango, a mobile-enabled submission form that hooks up to Salesforce.
You can also view leads in the system by navigating to yourapp.heroku.com/leads
Also, if you want to add in validation (and I suggest it) you can do this client-side rather than serverside.
Oh, and in case you were wondering the source code for all this is available on Github.

9 comments
Comments feed for this article
November 22, 2011 at 3:49 pm
Leopoldo
Great. Very useful..!!
November 29, 2011 at 8:31 pm
Wes
If you’re having issues with installing the ‘pg’ gem try running this first:
brew install postgresql
November 30, 2011 at 4:05 am
David
Loved your comments: “…ruby version management hell, gem dependency hell, or any other number of hells that brave would-be ninjas are occasionally trapped in. Should you end up here, prayer and Google may help you” This is so true my friend. I’ll let you know if I end up in hell! Cheers, David
December 5, 2011 at 5:04 pm
Terence Gannon
I worked through the steps above, and got the example working exactly as advertised. Cool. I then modified the example so I was able to access the Users object, and that worked as well. However, when I modified the code so as to access a Custom Object (called Request), I get the error; “uninitialized constant RequestsController::Request”. The offending line reads; “@requests = Request.all” Any chance there is a bug in the gem related to Customer Objects? Or is there something else I’m missing?
December 5, 2011 at 11:18 pm
Joel Dietz (@fractastical)
Custom objects usually have to have “__c” prepended to them. Try that?
December 5, 2011 at 11:22 pm
Terence Gannon
Yep, that was it. By using Requests__c, problem solved. Thanks for your help!
February 11, 2012 at 8:39 pm
Amit
I setup the application as described above, spent some time in fulfilling all the dependencies and eventually got the application running.
But my problem starts when is access the ‘/leads’ url. Even after hours of stuggle and googling, i can’t figure out what the problem is. Now ts rieally freaking me out.
Can any one provide some help on this. Following is a chunk from heroku logs. The error points to this line in controller : “@leads = Lead.all ”
Thanks in advance.
2012-02-11T20:32:50+00:00 app[web.1]: Started GET “/leads” for 59.164.193.105 at 2012-02-11 12:32:50 -0800
2012-02-11T20:32:50+00:00 app[web.1]: Processing by LeadsController#index as HTML
2012-02-11T20:32:50+00:00 app[web.1]: Completed 500 Internal Server Error in 1ms
2012-02-11T20:32:50+00:00 app[web.1]:
2012-02-11T20:32:50+00:00 app[web.1]: NoMethodError (undefined method `split’ for nil:NilClass):
2012-02-11T20:32:50+00:00 app[web.1]: app/controllers/leads_controller.rb:7:in `index’
2012-02-11T20:32:50+00:00 app[web.1]:
2012-02-11T20:32:50+00:00 app[web.1]: cache: [GET /leads] miss
2012-02-11T20:32:50+00:00 app[web.1]:
April 2, 2012 at 9:47 am
Ruby External CRON manager on Heroku with Sinatra, Resque and Redis « Tquila
[...] a couple of times on the web. The most recent and appealing posts and apps have been written by Fractastical, Metadaddy and Dburkes. They explain in a really readable way how to install, configure and use [...]
April 2, 2012 at 10:12 am
Ruby External CRON manager on Heroku with Sinatra, Resque and Redis « w3z.info
[...] a couple of times on the web. The most recent and appealing posts and apps have been written by Fractastical, Metadaddy and Dburkes. They explain in a really readable way how to install, configure and use [...]