Skip to content Skip to sidebar Skip to footer

How To Save Email To Subscribers With Checkbox?

When the box is checked how can I add the user's email from 'Enter Email' to my mailchimp subscribers list upon him clicking Save? user/new.html.erb <%= form_for(@user) do |f|

Solution 1:

The form is submitted to your App. So you can use the Mailchimp API to add the user to a list. There are various Mailchimp API gems. I just picked a random one:

Check here how to setup the API https://github.com/amro/gibbon

Signup Controller

defcreate@user = User.new(user_params)
  if@user.save
    subscribe_to_newsletter(@user)
    redirect_to ...
  else
    ...
  endend
private
defsubscribe_to_newsletter(user)
  gibbon.lists(list_id).members.create(body: {email_address: user.email, status:"subscribed", merge_fields: {FNAME: user.first_name, LNAME: user.last_name}})
end

Now this might take some time. If this is the case you might want to move it to a background job. Perhaps also move the whole Mailchimp code to a service object so it is properly encapsulated.

Post a Comment for "How To Save Email To Subscribers With Checkbox?"