如何使用复选框将电子邮件保存给订阅者?

How to save email to subscribers with checkbox?

选中此框后,如何在用户单击“保存”后将 'Enter Email' 中的用户电子邮件添加到我的 mailchimp 订阅者列表中?

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 %>

在注册过程之外,人们可以通过在 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>

表单已提交到您的应用程序。因此,您可以使用 Mailchimp API 将用户添加到列表中。有各种 Mailchimp API 宝石。我随便选了一个:

在此处查看如何设置 API https://github.com/amro/gibbon

注册控制器

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

现在这可能需要一些时间。如果是这种情况,您可能希望将其移至后台作业。也许还将整个 Mailchimp 代码移动到一个服务对象,以便正确封装。