0

我正在尝试提交一封电子邮件,该电子邮件将进入我创建的 Mailchimp 列表。问题是,当我在生产环境中单击订阅时,我会200 OK在控制台中收到一条消息:

[ee0cda43-9651-4c7e-9499-ac499094d8b6] Started POST "/subscribe" for 127.0.0.1 at 2017-08-26 02:52:00 -0700
[ee0cda43-9651-4c7e-9499-ac499094d8b6] Processing by SubscriptionsController#subscribe as JSON
[ee0cda43-9651-4c7e-9499-ac499094d8b6]   Parameters: {"email"=>"wlpz@gmail.com"}
[ee0cda43-9651-4c7e-9499-ac499094d8b6] Completed 200 OK in 2ms (Views: 0.4ms)

但是当我签入我的列表时,该电子邮件没有出现在我的联系人列表中。

顺便说一句,我使用 Ruby 2.3.1、Rails 5.1.2 和 Figaro 1.1.1 进行 ENV

这就是我在我的home.html.erb

<div class="subscribe">
    <div class="row">
      <div class="col-lg-12">
        <h4>Sign up for our Newsletter</h4>
        <div class="row small-uncollapse large-collapse">
          <div class="col-lg-11">
            <input id="email" placeholder="Your Email" type="text">/</input>
          </div>
          <div class="col-lg-1">
            <button class=".btn-primary" id="subscribe">Subscribe</button>
          </div>
        </div>
      </div>
    </div>
  </div>

routes.rb

get 'subscriptions/subscribe'
post :subscribe, controller: :subscriptions, action: :subscribe

使用 Gibbon 与 Mailchimp 进行 API 连接:

Gibbon::Request.api_key = ENV["MAILCHIMP_API_KEY"]
Gibbon::Request.timeout = 15
Gibbon::Request.open_timeout = 15
Gibbon::Request.symbolize_keys = true
Gibbon::Request.debug = false
puts "MailChimp API key: #{Gibbon::Request.api_key}" # temporary

并将 AJAX 的 JS 脚本集成到application.js

$(document).ready(function () {
    $('#subscribe').click(function () {
        var email = $('#email').val();
        $.ajax({
            url: '/subscribe',
            type: 'post',
            dataType: 'json',
            data: {email: email}
        })
        .done(function (data) {
            $('.subscribe').html('<strong>' + data.message + '</strong>');
        });

    });
});

我错过了什么吗?将不胜感激任何建议。

- - 更新 - -

订阅控制器:

class SubscriptionsController < ApplicationController
  def subscribe
    email = params[:email]
    gb = Gibbon::Request.new
    gb.lists.subscribe({:id => ENV["MAILCHIMP_LIST_ID"],
                        :email => {email: email}, :double_optin => false})
    render json: {message: "You are signed (#{email}) and good to go! Check your e-mail. Thank You!"}
  end
end

----更新2----

class SubscriptionsController < ApplicationController
  def subscribe
    email_address = params[:email_address]
    gb = Gibbon::Request.new
    gb.lists(ENV["MAILCHIMP_LIST_ID"]).members.create(body: {email_address: "email", status: "subscribed"})
    render json: {message: "You are signed (#{email_address}) and good to go! Check your e-mail. Thank You!"}
  end
end

更新dataapplication.js

data: {email_address: email}

错误日志:

[a6e21ee4-b955-4453-a1e0-56ff8035faee] Started POST "/subscribe" for 127.0.0.1 at 2017-08-27 04:25:33 -0700
[a6e21ee4-b955-4453-a1e0-56ff8035faee] Processing by SubscriptionsController#subscribe as JSON
[a6e21ee4-b955-4453-a1e0-56ff8035faee]   Parameters: {"email"=>"jlpz84@gmail.com"}
[a6e21ee4-b955-4453-a1e0-56ff8035faee] Completed 500 Internal Server Error in 956ms
[a6e21ee4-b955-4453-a1e0-56ff8035faee]   
[a6e21ee4-b955-4453-a1e0-56ff8035faee] Gibbon::MailChimpError (the server responded with status 400 @title="Invalid Resource", @detail="Please provide a valid email address.", @body={:type=>"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/", :title=>"Invalid Resource", :status=>400, :detail=>"Please provide a valid email address.", :instance=>""}, @raw_body="{\"type\":\"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",\"title\":\"Invalid Resource\",\"status\":400,\"detail\":\"Please provide a valid email address.\",\"instance\":\"\"}", @status_code=400):
[a6e21ee4-b955-4453-a1e0-56ff8035faee]   
[a6e21ee4-b955-4453-a1e0-56ff8035faee] app/controllers/subscriptions_controller.rb:8:in `subscribe'
4

1 回答 1

1

根据您的更新,我认为您很接近,但我认为:

body: {email_address: "email", status: "subscribed"}

应该:

body: {email_address: email_address, status: "subscribed"}

在某些情况下,我抓取了一些使用 Gibbonupsert方法的代码:

    Gibbon::Request.lists(@subscriber.mailing_list_info.list_id)
    .members(Digest::MD5.hexdigest(@subscriber.email))
    .upsert(
      body: {
        status_if_new: 'subscribed',
        email_address: @subscriber.email,
        merge_fields: @subscriber.mailing_list_info.merge_vars })

据我记得,如果成员存在,这将更新成员,如果不存在,则添加它们。

@subscriber并且@subscriber.mailing_list_info是我们用来包装User和添加相关 Mailchimp 信息的对象。

于 2017-08-27T13:35:45.147 回答