# Rendering OUTSIDE request

**URL:** <https://www.ruby-forum.com/t/rendering-outside-request/178039>\
**Category:** Rails\
**Created:** [October 28, 2009, 12:51pm UTC](https://www.ruby-forum.com/t/rendering-outside-request/178039 "2009-10-28T12:51:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [October 28, 2009, 12:51pm UTC](https://www.ruby-forum.com/t/rendering-outside-request/178039/1 "2009-10-28T12:51:36Z")

</div>

Hello,

I need to render erb template WITH LAYOUT OUTSIDE REQUEST, for example  
to generate document in background process.

How can I do this? I know that I can do ActionView::Base.new … but its  
possible to have also layout?

Code w/o layout support:

def return\_string\_to\_me\_baby

```
x = ActionView::Base.new(['app/views'])
 # assign template variables
assigns.each do |k,v|
  x.assigns[k] = v
end
x.render template

```

end

Thanks!

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [October 28, 2009, 12:53pm UTC](https://www.ruby-forum.com/t/rendering-outside-request/178039/2 "2009-10-28T12:53:04Z")

</div>

one more time 🙂

def return\_string\_to\_me\_baby(template, assigns={})

```
x = ActionView::Base.new(['app/views'])
 # assign template variables
assigns.each do |k,v|
  x.assigns[k] = v
end
x.render template

```

end

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [October 28, 2009, 2:13pm UTC](https://www.ruby-forum.com/t/rendering-outside-request/178039/3 "2009-10-28T14:13:09Z")

</div>

If there is somenthing like “render\_to\_string” - that you can use only  
in controller.

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [October 28, 2009, 2:57pm UTC](https://www.ruby-forum.com/t/rendering-outside-request/178039/4 "2009-10-28T14:57:56Z")

</div>

Quoting Frantisek P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> If there is somenthing like “render\_to\_string” - that you can use only  
> in controller.

You can use wget in a cronjob to invoke a controller request/method. If  
none  
of your users are on the same box, use local\_request? method to block  
invocation by other means. Or use some other method of  
authentication/authorization.

HTH,  
Jeffrey

P.S. local\_request? checks for 127.0.0.1 request IP address.

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [October 28, 2009, 3:23pm UTC](https://www.ruby-forum.com/t/rendering-outside-request/178039/5 "2009-10-28T15:23:06Z")

</div>

NICE IDEA, thanks! if i use this approach, it will also break other  
limitations/inconsistenties.

anyways, if someone knows, how to add layout-rendering capability (yield  
stuff) to my/your own code, i’am very curious

Jeffrey L. Taylor wrote:

> Quoting Frantisek P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > If there is somenthing like “render\_to\_string” - that you can use only  
> > in controller.
> 
> You can use wget in a cronjob to invoke a controller request/method. If  
> none  
> of your users are on the same box, use local\_request? method to block  
> invocation by other means. Or use some other method of  
> authentication/authorization.
> 
> HTH,  
> Jeffrey
> 
> P.S. local\_request? checks for 127.0.0.1 request IP address.
