我正在尝试提交一封电子邮件,该电子邮件会发送到我创建的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 for 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集成到application.js
中的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>');
});
});
});
我错过了什么吗?非常感谢任何建议。
---- ---- UPDATE
订阅控制器:
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
更新了data
中的application.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'
答案 0 :(得分:1)
根据您的更新,我认为您很接近,但我认为:
body: {email_address: "email", status: "subscribed"}
应该是:
body: {email_address: email_address, status: "subscribed"}
在某些情况下,我抓住了一些使用Gibbon的upsert
方法的代码:
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信息的对象。