【问题标题】:Mailchimp gem double_optin false not workingMailchimp gem double_optin false 不起作用
【发布时间】:2016-05-09 02:36:18
【问题描述】:

双重选择确认电子邮件仍然可以通过,知道出了什么问题吗?长臂猿宝石有问题,所以选择了mailchimp gem。

宝石文件

gem "mailchimp-api", "~> 2.0.4"

application_controller.rb

class ApplicationController < ActionController::Base

  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.

  protect_from_forgery with: :exception

  before_action :setup_mcapi

  def setup_mcapi
    @mc = Mailchimp::API.new('mailchimp api key goes here')
    @list_id = "list id goes here"
  end

end

welcome_controller.rb

class WelcomeController < ApplicationController
  def index
  end

  def subscribe
    email = params[:email][:address]

    if !email.blank?

      begin

        @mc.lists.subscribe(@list_id, {'email' => email}, 'double_optin' => false)
      end

      respond_to do |format|
        format.json{render :json => {:message => "Success!"}}
      end

    rescue Mailchimp::ListAlreadySubscribedError

      respond_to do |format|
        format.json{render :json => {:message => "#{email} is already subscribed to the list"}}
      end

    rescue Mailchimp::ListDoesNotExistError

      respond_to do |format|
        format.json{render :json => {:message => "The list could not be found."}}
      end

    rescue Mailchimp::Error => ex

      if ex.message

        respond_to do |format|
          format.json{render :json => {:message => "There is an error. Please enter valid email id."}}
        end

      else

        respond_to do |format|
          format.json{render :json => {:message => "An unknown error occurred."}}
        end
      end

    end

  else

    respond_to do |format|
      format.json{render :json => {:message => "Email Address Cannot be blank. Please enter valid email id."}}
    end

  end
end

end

index.html.erb

<h3>Add a New Member</h3>
<p>Please enter your email address to subscribe to our newsletter.</p>
<%= form_tag('/welcome/subscribe', method: "post", id: "welcome", remote: "true") do -%>
   <%= email_field(:email, :address, {id: "email", placeholder: "email address"}) %>
   <%= submit_tag("Subscribe") %>
<% end %>
<div id="response">Response Will be displayed here</div>

routes.rb

 Rails.application.routes.draw do
  root 'welcome#index'
  post 'welcome/subscribe' => 'welcome#subscribe'

end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 mailchimp


    【解决方案1】:

    来自mailchimp-api document

    订阅(id,电子邮件,merge_vars = nil,email_type = 'html',double_optin = true,update_existing = false,replace_interests = true,send_welcome = false)⇒ 哈希

    double_optin 是直接传递的参数,不像你做的那样。

    所以,它会是:

    @mc.lists.subscribe(@list_id, email, nil, 'html', false)
    

    【讨论】:

    • 我现在收到我尝试提交的任何电子邮件的 ex.message 错误。 rescue Mailchimp::Error =&gt; ex if ex.message respond_to do |format| format.json{render :json =&gt; {:message =&gt; "There is an error. Please enter valid email id."}} end
    【解决方案2】:

    上面的答案一直给我错误,这对我有用:

    @mc.lists.subscribe(@list_id, { "email" => email }, merge_vars = nil, email_type = 'html', double_optin = false)
    

    【讨论】:

    • 尝试解释这个答案与上面的答案有何不同。作为对其他答案的评论,这可能会更好。
    猜你喜欢
    • 2017-09-26
    • 2014-01-10
    • 2014-07-19
    • 2015-10-07
    • 2018-02-07
    • 1970-01-01
    • 2015-08-05
    • 2014-12-03
    • 2013-09-20
    相关资源
    最近更新 更多