简体   繁体   中英

customise Devise for Mailchimp

New to Rails and I am hooking up to the Mailchimp API through Gibbon.

I wanted to add subscribers to my app to a mailing list without them having to sign confirm twice. So I have added the API call to Mailchimp when a new User is created inside Devise::RegistrationsController. However, I guess it should actually be added once they confirm from the confirmation email. But I can't see where to do this correctly inside Devise::RegistrationsController. Here is my existing code...thanks.

class RegistrationsController < Devise::RegistrationsController


    def new
        super
    end

    def create

        super

        if resource.save

            # Add the new user email to Mailchimp
            # double-optin is part of the Mailchimp API that sends/doesn't send a confirmation email
            # in this case I'm already sending a confirm signup email
            # which already informs them they'll be added to a mailing list

            gb = Gibbon.new('my-mailchimp-api') 
            gb.list_subscribe({:id => 'my-mailchimp-list-id',
                               :email_address => resource.email, 
                               :merge_vars => {:FNAME => resource.forename, :LNAME => resource.surname }, 
                               :double_optin => "false"})               
        end

    end

    def edit
        super
    end

    def update
        super 
    end

    def destroy
        super 
    end

    def cancel
        super
    end

    protected

    def update_needs_confirmation?(resource, previous)
        super
    end

    def build_resource(hash=nil)
        super
    end

    def sign_up(resource_name, resource)
        super
    end

    def after_sign_up_path_for(resource)
        super
    end

    def after_inactive_sign_up_path_for(resource)
        super
    end

    def after_update_path_for(resource)
        super  
    end

    def authenticate_scope!
        super
    end

    def sign_up_params
        super
    end

    def account_update_params
        super
    end
end

This is quite easy to achieve, you can do it by overriding the confirm! method in your user model:

def confirm!
  super
  do_some_mailchimp_stuff
end

Hope this helps!

You can use devise_mailchimp gem. It's available here https://github.com/jcnnghm/devise_mailchimp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM