Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save email to subscribers with checkbox?

When the box is checked how can I add the user's email from 'Enter Email' to my mailchimp subscribers list upon him clicking Save?

enter image description here

user/new.html.erb

<%= form_for(@user) do |f| %>
  <%= f.email_field :email, placeholder: 'Enter Email' %>
  <%= f.check_box ????? %> Get blog posts from Anthony Galli, CEO & Founder about conquering challenges in life & business!
<% end %>

Outside of the signup process people can subscribe via inputting their email in subscribes/subscribe.html.erb.

<!-- MailChimp Signup Form -->
<link href="//cdn-images.mailchimp.com/embedcode/classic-081711.css" rel="stylesheet" type="text/css">
<div id="mc_embed_signup">
  <form action="//anthonygalli.us8.list-manage.com/subscribe/post?u=3e4b26579d28ecaf37fe444e4&amp;id=3dbb9c5c12" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
    <input type="email" value="" name="EMAIL" class="required email" placeholder="Enter Email" id="mce-EMAIL">
    <input type="text" name="b_3e4b26579d28ecaf37fe444e4_3dbb9c5c12" tabindex="-1" value="">
    <input type="submit" value="Save" name="subscribe" id="mc-embedded-subscribe" class="button">
  </form>
</div>
<script type='text/javascript' src='//s3.amazonaws.com/downloads.mailchimp.com/js/mc-validate.js'></script><script type='text/javascript'>(function($) {window.fnames = new Array(); window.ftypes = new Array();fnames[0]='EMAIL';ftypes[0]='email';fnames[1]='FNAME';ftypes[1]='text';fnames[2]='LNAME';ftypes[2]='text';}(jQuery));var $mcj = jQuery.noConflict(true);</script>
like image 621
AnthonyGalli.com Avatar asked Dec 02 '25 13:12

AnthonyGalli.com


1 Answers

The form is submitted to your App. So you can use the Mailchimp API to add the user to a list. There are various Mailchimp API gems. I just picked a random one:

Check here how to setup the API https://github.com/amro/gibbon

Signup Controller

def create
  @user = User.new(user_params)
  if @user.save
    subscribe_to_newsletter(@user)
    redirect_to ...
  else
    ...
  end
end
private
def subscribe_to_newsletter(user)
  gibbon.lists(list_id).members.create(body: {email_address: user.email, status: "subscribed", merge_fields: {FNAME: user.first_name, LNAME: user.last_name}})
end

Now this might take some time. If this is the case you might want to move it to a background job. Perhaps also move the whole Mailchimp code to a service object so it is properly encapsulated.

like image 147
Pascal Avatar answered Dec 05 '25 04:12

Pascal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!