Gareth Burrows
Gareth Burrows

Reputation: 41

Rails submit html form in addition to model form

I'm fairly new to rails with very small knowledge of javascript. That's the disclaimer :)

I'm using simple form and haml in a rails app to create an instance of a model. The code is simple, and works well. It's essentially this...

= simple_form_for @account, :validate => true, :url => { :action => 'create' } do |account|
  = account.input :name, :placeholder => "Company name", :label => false
  = account.input :first_name, :placeholder => "First name", :label => false
  = account.input :last_name, :placeholder => "Last name", :label => false
  = account.submit "Create my account"

I have simplified the above.

My sales team have come to me and asked me to create a lead in salesforce every time a new account is created. Salesforce have functionality at the cheap end called web to lead, which is essentially a web form. Again, a simplified version of the form looks like this...

<form action="https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">
<input type=hidden name="oid" value="00Db0000000bAtK">
<input type=hidden name="retURL" value="http://www.breathehr.com/blog/">
<label for="first_name">First Name</label><input  id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>
<label for="last_name">Last Name</label><input  id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>
<label for="company">Company</label><input  id="company" maxlength="40" name="company" size="20" type="text" /><br>
<input type="submit" name="submit">
</form> 

now, my thoughts are that I can populate this form with variables from the screen and then somehow get the rails simple_form to submit the html form when it is submitted. I have access to guys who know java and ajax if necessary, but I'm looking for the best and easiest solution.

so my two questions are

  1. Is it possible to have a hidden form and have the simple_form submit button submit both forms
  2. what's the best approach to this problem

Upvotes: 0

Views: 172

Answers (1)

Jason Swett
Jason Swett

Reputation: 45084

I think the way I would do it, if possible, is this:

  1. Create the account in Rails. Don't worry in this step about Salesforce.
  2. After the account is created in Rails, create the lead in Salesforce. I probably wouldn't go the HTML form route with this. It looks like Salesforce has a REST API, so I would probably use that. One option would be to create an :after_create hook in your Account model that makes the API call. Another option would be to make the API call from the AccountsController right after the account is saved.

Hopefully that gets you a little further down the road.

Edit: If you can't use the API, my advice is the same except replace "make API call" with "send HTTP request to Salesforce." The HTTP request would have oid, retURL, first_name, last_name, etc. I would handle making the HTTP request on the server side as opposed to trying to make both forms "submit at the same time" or anything like that.

If you need guidance on how to do the HTTP request part, I'd recommend starting a separate question for that. Hint: as a first step, create a standalone mini-project that sends an HTTP request to Salesforce and nothing more. Once you have that working, incorporate it into the larger program.

Upvotes: 1

Related Questions