You are currently browsing the category archive for the ‘Ruby on Rails’ category.

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.

@fractastical updates

 

June 2012
M T W T F S S
« May    
 123
45678910
11121314151617
18192021222324
252627282930  
Follow

Get every new post delivered to your Inbox.

Join 1,312 other followers