0

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.

Ravi
  • 304
  • 2
  • 15
  • I just misspelled it in the question. It is right in my code. – Ravi Nov 25 '15 at 21:45
  • Don't misspell it in the question, fix it so it's correct when we read it. Also, fix the formatting of your code so it's readable. As is, it's not easy to read, which slows and even discourages answers. – the Tin Man Nov 25 '15 at 21:51

1 Answers1

1

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
...
linkyndy
  • 17,038
  • 20
  • 114
  • 194
  • Sorry, but I am a bit confused. – Ravi Nov 25 '15 at 21:48
  • When I added `ActiveRecord::Base` to the model and removed `@subscriber.valid?` and `@subscriber.subscribe` it worked perfectly but gives the error `uninitialized constant Gibbon::Request` when I add both of them back. Now when I add `include ActiveModel::Validations` or `include ActiveModel::Model` instead of `ActiveRecord::Base` I still get the uninitilized error. – Ravi Nov 25 '15 at 21:54
  • You don't have to remove any of those methods; by adding `ActiveRecord::Base` as the superclass you actually _make_ them work. If you removed `@subscriber.subscribe` though, why would it throw you that error since you don't call `Gibbon::Request` anywhere? – linkyndy Nov 25 '15 at 21:57
  • It still isn't working. I am still getting the `uninitialized constant Gibbon::Request` error. – Ravi Nov 25 '15 at 22:06
  • When I typed Gibbon, console replied with `Gibbon` and when I do `Gibbon::Request`, I get this `NameError: uninitialized constant Gibbon::Request`. – Ravi Nov 25 '15 at 22:12
  • That seems odd. When the gem is loaded, I see it requires `Gibbon::Request` also. Are you using the latest gem version? Check that you didn't pin the version in `Gemfile` and also run a `bundle update gibbon`. – linkyndy Nov 25 '15 at 22:16
  • Updating my gems fixed it. Thanks a lot Andrei. But I am getting that the user is subscribed in the Rails logger but my mailchimp list does not show any subscribed user. And it is also not logging my API key requests. – Ravi Nov 25 '15 at 22:29
  • Check that you're setting the correct API key. I guess we should answer only the initial question, otherwise this SO topic will spread too much. I guess it's better to open a separate question for each of your issues. – linkyndy Nov 26 '15 at 11:19