Reputation: 4583
I am trying to use gibbon gem and following their github docs. Here is the code I have
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 %>
The exact error message is
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'
Upvotes: 0
Views: 1231
Reputation: 4643
The error message you're getting back is telling you exactly what the problem is:
{
"field": "email_address",
"message": "Schema describes string, object found instead"
}
You're passing the email in as a javascript object (ruby hash) instead of a string. All you need to pass is the raw email address.
Upvotes: 2
Reputation: 665
I think you need to give the members
method a MD5 hash of the lower case email address (via the mailchimp subscriber management). Try
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
Upvotes: 1