0

我正在尝试使用 gibbon gem 并关注他们的 github 文档。这是我的代码

class ChimpController < ApplicationController
  def create
    send_to_mail_chimp ( params[:email])
  end

  def send_to_mail_chimp(email)
    puts "send email is #{email}"
    gibbon = Gibbon::Request.new(api_key: "bla")
    gibbon.timeout = 10
    gibbon.lists('e61cf2454d').members.create(body: {email_address: email, status: "subscribed"})
  end
end


<%= simple_form_for :email, url: newsletter_path, :method => :post do |f| %> <%= f.input :email, input_html: {class: 'form-control', placeholder: 'enter email'} %> <% end %>

确切的错误信息是

Gibbon::MailChimpError (the server responded with status 400 @title="Invalid Resource", @detail="The resource submitted could not be validated. For field-specific details, see the 'errors' array.", @body={"type"=>"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/", "title"=>"Invalid Resource", "status"=>400, "detail"=>"The resource submitted could not be validated. For field-specific details, see the 'errors' array.", "instance"=>"", "errors"=>[{"field"=>"email_address", "message"=>"Schema describes string, object found instead"}]}, @raw_body="{\"type\":\"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",\"title\":\"Invalid Resource\",\"status\":400,\"detail\":\"The resource submitted could not be validated. For field-specific details, see the 'errors' array.\",\"instance\":\"\",\"errors\":[{\"field\":\"email_address\",\"message\":\"Schema describes string, object found instead\"}]}", @status_code=400):
  app/controllers/chimp_controller.rb:10:in `send_to_mail_chimp'
  app/controllers/chimp_controller.rb:3:in `create'
4

2 回答 2

2

您收到的错误消息准确地告诉您问题出在哪里:

{
    "field": "email_address",
    "message": "Schema describes string, object found instead"
}

您将电子邮件作为 javascript 对象(ruby 哈希)而不是字符串传递。您需要传递的只是原始电子邮件地址。

于 2016-03-21T16:05:21.190 回答
1

我认为您需要为该members方法提供小写电子邮件地址的 MD5 哈希(通过 mailchimp 订阅者管理)。尝试

def send_to_mail_chimp(email)
  puts "send email is #{email}"
  gibbon = Gibbon::Request.new(api_key: "bla")
  gibbon.timeout = 10
  md5_email = Digest::MD5.hexdigest(email['email'].downcase)
  # I prefer 'upsert' to 'create' but should work with either
  gibbon.lists('e61cf2454d').members(md5_email).upsert(body: {email_address: email['email'], status: "subscribed"})
end
于 2016-03-21T15:48:45.460 回答