Rendering a Rails view from the Sidekiq worker face

I have a Sidekiq worker doing some background processing and then finally sending a successful message to a third party API. This post I am posting is essentially a "thank you" message and may contain HTML.

In a post that I would like to link to my site in a properly formatted way. This naturally sounds like looking at me. I would like to just use a view template, render it in HTML, and finally publish it to the API.

In my life, I cannot figure out how to pretend my Sidekiq worker.

I thought about creating a dummy controller / view compiler and instantiating it from within a worker, but it seems to be wrong.

Any input is greatly appreciated.

+3


source to share


2 answers


Inside your worker, you can use ActionView :: Base directly to render the view. For example, to render users/events

partial:



view = html = ActionView::Base.new(Rails.root.join('app/views'))
view.class.include ApplicationHelper
view.render(
  partial: 'users/event', 
  object: user.events.last
)

      

+6


source


You can use ERB

renderer for template in your job.

require 'erb'

@text = "my answer" # variables, accessible in template.
template = "This is <%= @text %>."

# you can also render file like ERB.new(File.read(<file name here>))
renderer = ERB.new(template)
puts output = renderer.result()

      



More about ERB

+2


source







All Articles