Gibbon :: MailChimpError(服务器响应状态为400 @title ="无效资源"

时间:2016-03-21 15:10:50

标签: ruby-on-rails mailchimp gibbon

我正在尝试使用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'

2 个答案:

答案 0 :(得分:2)

您收到的错误消息正在告诉您具体问题:

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

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

答案 1 :(得分:1)

我认为您需要为members方法提供小写电子邮件地址(via the mailchimp subscriber management)的MD5哈希值。尝试

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