title: trigger a function from a button click in ruby
body: <p>I have a ruby on rails app with a &quot;contact us&quot; form and I would like it to trigger my sendgrid function to send an email everytime the submit button is clicked on my form.</p>
  <p>Right now it only sends the email everytime I refresh the page.</p>
  <p>this is my button</p>
  <pre><code>                            &lt;div class=&quot;row&quot;&gt;
                                  &lt;div class=&quot;col-md-12&quot;&gt;
                                      &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;&lt;i class=&quot;fa fa-check&quot;&gt;&lt;/i&gt; SEND MESSAGE&lt;/button&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
  </code></pre>
  <p>and this is my sendgrid function that creates and email</p>
  <pre><code>def sendgrid(lead)
      mail = Mail.new
      mail.from = Email.new(email: 'blabalabl@gmail.com')
      custom = Personalization.new
      custom.add_to(Email.new(email: lead.email))
      custom.add_dynamic_template_data({
          &quot;fullName&quot; =&gt; lead.fullNameContact,
          &quot;projectName&quot; =&gt; lead.nameProject
      })
      mail.add_personalization(custom)
      mail.template_id = 'd-d25e6394d3434930835982c01f5eb980'
  
      testing1 = SendGrid::API.new(api_key: ENV['SENDGRID_APIKEY'])
  
      response = testing1.client.mail._('send').post(request_body: mail.to_json)
      
  end
  </code></pre>