# Render partial from Backgroundrb worker?

**URL:** <https://www.ruby-forum.com/t/render-partial-from-backgroundrb-worker/112827>\
**Category:** Rails\
**Created:** [August 23, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/render-partial-from-backgroundrb-worker/112827 "2007-08-23T16:59:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [August 23, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/render-partial-from-backgroundrb-worker/112827/1 "2007-08-23T16:59:19Z")

</div>

Hi,

I have a long running task which generates JavaScript to files and I am  
hoping to move this to a backgroundrb worker. The javascript is  
currently generated with call to render\_to\_string:

File.open(File.join(target\_folder, ‘all.js’), “w”) do |file|  
file.puts(render\_to\_string :partial =\>  
‘partials/shared/javascript/tags’, :locals =\> { :namespaces =\>  
@namespaces, :tags =\> @tags, :parent\_tags =\> @parent\_tags, :show\_counts  
=\> true })  
end

It seems that the render\_to\_string is not available to the worker. Does  
anyone know of a better way to achieve this using a partial? I can fall  
back to generating the javascript text in a method, but this seems quite  
an ugly solution (although will be much more performant than my current  
in process solution).

Any tips or pointers would be much appreciated.

Thanks,  
GiantCranes

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [August 24, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/render-partial-from-backgroundrb-worker/112827/2 "2007-08-24T12:09:47Z")

</div>

Dan,

Thanks! That worked well. The only slight modification was the addition  
of the following lines:

require ‘active\_support’  
require ‘action\_controller’  
require ‘action\_view’

Thanks again,  
GiantCranes

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [August 24, 2007, 2:05am UTC](https://www.ruby-forum.com/t/render-partial-from-backgroundrb-worker/112827/3 "2007-08-24T02:05:22Z")

</div>

It is possible to render an action from backgroundrb, although it’s a  
bit tedious. Backgroundrb doesn’t seem to load the controller layer  
when it runs - I assume this is because it’s intended for manipulating  
data, and not generating views, which makes sense. Anyway, in order to  
get the rendering functions working you need to create a Dummy  
controller that implements a few methods that render relies on. I’ve  
included a stripped down version below which _should_ work, although I  
haven’t tested it. The example works on the older win32 compatible  
version of backgroundrb, but I haven’t tried it on the newer \*nix only  
version.

====================================================

class SampleWorker \< BackgrounDRb::Rails  
require ‘action\_view’

def do\_work(args)  
#redirect the rails default logger for active record to  
backgroundrb  
ActiveRecord::Base.logger= @logger

```
action_view =

```

ActionView::Base.new(Rails::Configuration.new.view\_path, {},  
DummyController.new)  
action\_view.render(:partial =\> “foo/bar”)  
end  
end

class DummyController

def logger  
@logger  
end

def headers  
{‘Content-Type’ =\> ‘application/xml’}  
end

def response  
DummyResponse.new  
end

def content\_type  
‘application/xml’  
end

end

class DummyResponse  
def content\_type  
‘application/xml’  
end  
end

====================================================  
Hope that helps,

Dan
