Search code examples
ruby-on-railsrubymailchimpgibbon

Undefined method and uninitialized constant error


Hey guys I am trying integrate Mailchimp API using gibbon gem but I keep getting an error undefined methodsave'`.

My controller code:

class LandingPageController < ApplicationController

layout 'landing_page'

def index

@subscriber = Subscriber.new

end




def create
    # Instantiate a new object using form parameters

    @subscriber = Subscriber.new(subscriber_params)

    # Save the object
    if @subscriber.save 
      @subscriber.valid?
      @subscriber.subscribe
      # If save succeeds, redirect to the index action
      flash[:notice] = "Thank You for subscribing. Your Email ID has been entered successfully into our database."

      redirect_to(:action => 'index')
    else
      # If save fails, redisplay the form so user can fix problems

      render('index')
    end
  end







  private

    def subscriber_params
      # same as using "params[:subject]", except that it:
      # - raises an error if :subject is not present
      # - allows listed attributes to be mass-assigned
      params.require(:subscriber).permit(:email)
    end

end

This was working perfectly before started the integration. So to debug the error I removed the @subscriber.save to see what happens. Then I get a new error uninitialized constant Gibbon::Request in subscriber.rb. My subscriber.rb code:

class Subscriber
  include ActiveModel::Model
  attr_accessor :email, :string
  validates_presence_of :email
  validates_format_of :email, with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i

  def subscribe
    mailchimp = Gibbon::Request.new(api_key: Rails.application.secrets.mailchimp_api_key)

    result = mailchimp.lists.subscribe({
      :id => Rails.application.secrets.mailchimp_list_id,
      :email => {:email => self.email},
      :double_optin => false,
      :update_existing => true,
      :send_welcome => true
    })
    Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
  end


end

I have tried atleast a dozen different fixes. I have tried different versions of gibbon gem, nothing seems to work. I have tried hardcoding my API key instead of getting it from secrets.yml. I have run out of options now and other questions on google. There doesn't seem to be solution to my problem.

Would appreciate any help possible and let me know if you need any other code from my project.

Edit:

I was able to remove the undefined method save error by adding ActiveRecord::Base instead of include ActiveModel::Model. But I still get the uninitialized constant Gibbon::Request error.


Solution

  • There are several issue with your code:

    • if your error was uninitialized constant Gibbon::Resquest in subscriber.rb, then probably you've misspelled Request;
    • save is an ActiveRecord method, not an ActiveModel one. So your model should inherit from ActiveRecord::Base instead of including ActiveModel::Model;
    • your validations won't work unless you include ActiveModel::Validations.

    These being said, there is no problem with the gem you use. If you add the correct superclass to your model and remove the include, things should work as expected for you.

    Regarding the persistence of the constant not being found, have you added gem 'gibbon' in your Gemfile and run bundle afterwards? To check, fire up a Rails console and see if the gem is loaded:

    $ rails c
    >> Gibbon
    ...
    >> Gibbon::Request
    ...