user2934075
user2934075

Reputation: 11

Using erb template and sidekiq

I'm using sinatra and sidekiq together. I'm trying to render a erb template from within a sidekiq worker and i'm getting a undefined method 'erb'. In my head, things should work cause sidekiq is loaded up as an instance of my sinatra app, so it should have the erb method. What am i missing here?

class SomeWorker
  include Sidekiq::Worker

  def perform(id)
    erb(:emailTemplate)
  end
end

(i now realized the SomeWorker class has nothing to to do with sinatra and so of course it doesn't have the erb method. maybe i can just make a call out to helper module? in place of the erb method call?)

Upvotes: 1

Views: 322

Answers (2)

Fahad Abbas
Fahad Abbas

Reputation: 1

So here is a workaround you can try:

class MyWorker
  include Sidekiq::Worker

  def perform
    template_path = 'path/to/template.erb'
    template_content = File.read(template_path

    erb_template = ERB.new(template_content)
    result = erb_template.result(binding)
    puts result
  end
end

Upvotes: 0

user2934075
user2934075

Reputation: 11

so i figured out 1 solution:

require 'erb'
require 'tilt'

class ForgotEmailWorker
  include Sidekiq::Worker

  def perform
    template = tilt.new(__path_to_erb_file).render
  end
end

Upvotes: 0

Related Questions