In my app's user settings, a user can subscribe or unsubscribe to a MailChimp list by checking a box. I am having some trouble retrieving the users MailChimp member_id.
I created a .env file with:
MAILCHIMP_API_KEY = "my_api_key"
MAILCHIMP_LIST_ID = "my_list_id"
I created an initializer called gibbon.rb with:
Gibbon::Request.api_key = ENV["MAILCHIMP_API_KEY"]
Gibbon::Request.timeout = 15
I use this method in the user.rb
def mailchimp_news
@mailchimp_list_id = ENV["MAILCHIMP_LIST_ID"]
@gibbon = Gibbon::Request.new
if self.news_email == true
@gibbon.lists(@mailchimp_list_id).members.update(body: {
email_address: self.email,
status: "subscribed",
merge_fields: {FNAME: self.name,
LNAME: ""}
})
elsif self.news_email == false
@gibbon.lists(@mailchimp_list_id).members(member_id).update(body: { status: "unsubscribed" })
end
end
And call it with an after check
after_save :mailchimp_news
I receive this error: undefined local variable or method `member_id'
. How do I get the member_id?