javascript – 如何通过复选框将电子邮件保存到订阅者?

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

javascript  – 如何通过复选框将电子邮件保存到订阅者?

用户/ 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代码移动到服务对象,以便正确封装.

上一篇:javascript – 如何更改subscribe函数内的observable值?


下一篇:angular7中常见细节使用