我正在尝试提交一封电子邮件,该电子邮件将进入我创建的 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
更新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'