Search code examples
ruby-on-rails-3salesforcedatabasedotcom-gem

Routes error on nested controller when submitting form, databasedotcom-gem


I am using the databasedotcom & databasedotcom-rails gem so that I get generate leads into Salesforce from form submissions. It was working standalone until I nested the controller under a parent, and now when I press submit I get this routes error:

No route matches [POST] "/events/516ee9a0421aa9c44e000001/leads/new"

Here is all my code:

resources :events do
  resources :leads
end

class LeadsController < ApplicationController
   include Databasedotcom::Rails::Controller

  def new
    @lead = Lead.new
    respond_to do |format|
    format.html # new.html.erb
    format.json { render :json => @lead }
    end
 end

 def create
   @lead = Lead.new(params[:lead])
   @lead.event_id = params[:event_id]
   @lead['OwnerId'] = '005b0000000WxqE'   
   @lead['IsConverted'] = false
   @lead['IsUnreadByOwner'] = false
   respond_to do |format|
   if @event_lead.save
    format.html { redirect_to @lead, :notice => 'Lead was successfully created.' }
    format.json { render :json => @lead, :status => :created, :location => @lead }
  else
    format.html { render :action => "new" }
    format.json { render :json => @lead.errors, :status => :unprocessable_entity }
  end
  end
  end
end   

<%= simple_form_for [@event, @lead], url: new_event_lead_path do |f| %>
  <%= f.input :Download_Brochure__c, :check => "true",  :as => :boolean, :as => :hidden %>
  <%= f.input :FirstName %>
  <%= f.input :LastName %>
  <%= f.input :Company %>
  <p>Also interested in:</p>
  <%= f.input :Sponsor_Request__c, :as => :boolean, :label => "Sponsoring" %>
  <%= f.input :Presenting__c, :as => :boolean, :label => "Presenting" %>
  <%= f.input :Newsletter_Signup__c, :as => :boolean, :label => "Newletter" %>
  <%= f.input :Privacy_Policy__c, :as => :boolean, :checked => true, :label => "Would you like to stay updated" %>
  <%= f.button :submit, :label => "Submit" %>
<% end %>

Solution

  • The problem is in your form. With the routes as you have written them, the new_event_lead_path maps to a GET request to your LeadsController#new action. Run rake routes on the command line to confirm this.

    You want to submit the form to LeadsController#create. Rails will set this up for you when you use a new instance of Lead in the expression simple_form_for [@event, @lead] provided you don't override the url. Therefore, update your form:

    <%= simple_form_for [@event, @lead] do |f| %>