从Sidekiq Worker中渲染Rails视图 [英] Rendering a Rails view from within a Sidekiq Worker

查看:74
本文介绍了从Sidekiq Worker中渲染Rails视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Sidekiq工人,该工人进行一些后台处理,然后最终将成功消息发布到第三方API.我发布的该消息实质上是谢谢"消息,并且可以包含HTML.

I have a Sidekiq worker that does some background processing and then finally POSTs a success message to a 3rd party API. This message I post is essentially a "thank you" message and can contain HTML.

在消息中,我想以正确的格式链接回我的网站.这自然听起来像是对我的看法.我很想简单地使用视图模板,将其呈现为HTML,最后将其发布到API.

In the message I'd like to link back to my site in a properly formatted way. This naturally sounds like a view to me. I'd love to simply use a view template, render it to HTML and finally post it off to the API.

对于我的一生,我无法弄清楚如何从Sidekiq工作人员内部渲染视图.

For the life of me, i cannot figure how to render a view from within my Sidekiq worker.

我已经考虑过设置一个虚拟控制器/视图组合,并从worker内部实例化它,但这似乎是错误的.

I have considered setting up a dummy controller/view combo and instantiating it from inside the worker but it seems wrong.

任何输入将不胜感激.

推荐答案

在工作人员内部,您可以直接使用ActionView :: Base渲染视图.例如,要呈现 users/events 部分:

Inside your worker you can use ActionView::Base directly to render a view. For example, to render a 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
)

这篇关于从Sidekiq Worker中渲染Rails视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆