I am stuck and even if I found some subjects about this issue, I didn't found any solution.
I am trying to add a subscription to Mailchimp if my "Packer" register to our newsletter (A "Packer" is a kind of "User" - "User" only has the Devise parameters and "Packer" has the rest)
The error is an uninitialized constant, It seems to be because rails doesn't recognize my service.
Here is my #app/models/packer.rb
after_create :subscribe_to_newsletter
after_update :subscribe_to_newsletter
private
def subscribe_to_newsletter
SubscribeToNewsletterService.new.call(self.user) if self.newsletter
end
and my # app/services/subscribe_to_newsletter_service.rb
require "gibbon"
class SubscribeToNewsletterService
def initialize
@gibbon = Gibbon::Request.new(api_key: ENV['MAILCHIMP_API_KEY'])
@list_id = ENV['MAILCHIMP_NEWSLETTER_LIST_ID']
end
def call(user)
@gibbon.lists(@list_id).members.create(
body: {
email_address: user.email,
status: "subscribed",
double_optin: false,
# merge_fields: {
# FNAME: @user.first_name,
# LNAME: @user.last_name
# }
}
)
end
end
Looking into the different solutions, I also added that line in #config/application.rb
module Pyswebsitev1
class Application < Rails::Application
@config.autoload_paths += %W(#{config.root}/app/services)
config.i18n.default_locale = :en
end
end
When I do a rails console - ActiveSupport::Dependencies.autoload_paths The result include /app/services
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/assets",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/channels",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/controllers",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/controllers/concerns",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/helpers",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/jobs",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/mailers",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/models",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/models/concerns",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/policies",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/app/services",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails-assets-underscore-1.8.3/app/assets",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jquery-fileupload-rails-0.4.7/app/assets",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/attachinary-98a895be22ed/app/controllers",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails_admin-1.2.0/app/assets",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails_admin-1.2.0/app/controllers",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rails_admin-1.2.0/app/helpers",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/jquery-ui-rails-5.0.5/app/assets",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/font-awesome-rails-4.7.0.2/app/assets",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/font-awesome-rails-4.7.0.2/app/helpers",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.3.0/app/controllers",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.3.0/app/helpers",
"/usr/local/Cellar/rbenv/1.0.0/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.3.0/app/mailers",
"/Users/MaxBook/code/PackYourSkills/pyswebsitev1/test/mailers/previews"
I also tried to "Spring stop", to reboot server, and to bundle.
If you have any idea, that would be very helpfull ! Thank you very much
Maybe you forgot to put the
_service
in your filename event if you put it in your question. Just guessing ...