我正在尝试在 Rails 中包含带有 Gibbon gem 的电子邮件表单的 MailChimp Api。
添加邮件等工作,我在控制台中看到来自 API 的正确回调,但除了成功案例,它正确更改了电子邮件 id div,没有任何反应。
似乎 case 开关不起作用,或者它无法读取从 api 返回的代码。
路线等设置正确。
这是我的控制器:
class MailchimpformController < ApplicationController
def index
end
def submit
mailchimp_api_key = "mysecretapikey"
mailchimp_list_id = "mysecretlistid"
g = Gibbon.new(mailchimp_api_key)
response = g.list_subscribe({:id => mailchimp_list_id,
:email_address => params[:email],
:double_optin => false,
:send_welcome => false})
if(response.is_a?(Hash))
puts response
case response['code']
when 502
@js_email_error = "Invalid Address!"
when 214
@js_email_error = "Already signed up!"
else
@js_email_error = response['error']
end
@js_email_success = nil
else
@js_email_success = "Thanks!"
@js_email_error = nil
end
respond_to do |format|
format.js
end
end
end
它仅在将新电子邮件添加到列表时才有效。
index.html.erb
<%= form_tag submit_path, :class=> "form", remote: true do %>
<%= text_field_tag :email, nil, :class => 'email', :type=>"email", :placeholder => 'Sign up for beta testing' %>
<%= submit_tag "Absenden", :alt => "Absenden", class: "input-btn"%>
<% end %>
和 javascript 的东西 submit.js.erb
<% if @js_email_error %>
$("#email").val("<%= @js_email_error %>");
<% end %>
<% if @js_email_success %>
$("#email").val("<%= @js_email_success %>");
<% end %>
我还收到了Completed 500 Internal Server Error
来自 MailChimp API 的带有(正确)回调的 POST /mailchimpform/submit 。我不知道为什么。