# Rendering a view outside of controller?

**URL:** <https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949>\
**Category:** Rails\
**Created:** [March 26, 2008, 2:13am UTC](https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949 "2008-03-26T02:13:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [March 26, 2008, 2:13am UTC](https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949/1 "2008-03-26T02:13:10Z")

</div>

Is there anyway I can render a view outside of a controller ?

I’m using BackgroundRB , and I’d like to use render\_to\_string in a  
worker to render some html that will update a facebook profile. These  
are perodic updates and so these must happen in a scheduled process  
rather than in real time from a controller.

The only other option I can think of is just to create an action that my  
worker calls with the appropriate values and ensure that the request is  
only allowed locally. I’d prefer not to do this as it may tie up some of  
my web server processes…

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [March 26, 2008, 10:08am UTC](https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949/2 "2008-03-26T10:08:25Z")

</div>

On Wed, Mar 26, 2008 at 6:43 AM, Dave F.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## only allowed locally. I’d prefer not to do this as it may tie up some of my web server processes…

I haven’t tried myself,but you have access to entire rails environment  
in your worker, hence you can use render\_to\_string easily.  
Also, You probably want the rendered result to be saved in database or  
result hash.

–  
Let them talk of their oriental summer climes of everlasting  
conservatories; give me the privilege of making my own summer with my  
own coals.

[http://gnufied.org](http://gnufied.org)

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [March 26, 2008, 3:22pm UTC](https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949/3 "2008-03-26T15:22:47Z")

</div>

I don’t know if this is something you have considered, but using wget  
in cron locally might be another option.

H

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [March 26, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949/4 "2008-03-26T23:34:07Z")

</div>

blasterpal wrote:

> I don’t know if this is something you have considered, but using wget  
> in cron locally might be another option.

Yeah, that’s what I was implying in my first post

“The only other option I can think of is just to create an action that  
my  
worker calls with the appropriate values and ensure that the request is  
only allowed locally.”

Which is what I’ve done for the mean time. Using BackgroundRB to  
schedule a worker which uses Net::HTTP to do a get request to an action  
on my application. I decided to keep with backgroundrb to do this (as  
opposed to cron and wget) as I"ve got other processes which happen so  
this keeps things consistent.

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [March 27, 2008, 12:28am UTC](https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949/5 "2008-03-27T00:28:22Z")

</div>

Well this may not be a very elegant solution(or may not even be viable)  
but  
you can probably do this

def get\_binding  
binding  
end

File.open("#{RAILS\_ROOT}/app/views//", “r”){|f|  
template=f.readlines.to\_s  
}  
rhtml = ERB.new(template)  
str=rhtml.result(self.get\_binding)  
out.write str #your rhtml rendered as html

instead of using render\_to\_string to render some html

On Thu, Mar 27, 2008 at 4:04 AM, Dave F. \<

---

<div class="post-metadata">

**Author:** ![analogue40](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@analogue40](https://www.ruby-forum.com/u/analogue40)\
**Post date:** [March 26, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/rendering-a-view-outside-of-controller/132949/6 "2008-03-26T23:31:24Z")

</div>

Hemant K. wrote:

> On Wed, Mar 26, 2008 at 6:43 AM, Dave F.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ## only allowed locally. I’d prefer not to do this as it may tie up some of my web server processes…
> 
> I haven’t tried myself,but you have access to entire rails environment  
> in your worker, hence you can use render\_to\_string easily.  
> Also, You probably want the rendered result to be saved in database or  
> result hash.
> 
> –  
> Let them talk of their oriental summer climes of everlasting  
> conservatories; give me the privilege of making my own summer with my  
> own coals.
> 
> [http://gnufied.org](http://gnufied.org)

Yes I’ve got access to the environment but as render\_to\_string is  
defined in ActionController I have to instantiate this either from base  
or an existing controller … when I do this it gives me an from  
render\_to\_string when it calls ‘erase\_render\_results’:

```
  def erase_render_results #:nodoc:
    response.body = nil
    @performed_render = false
  end

```

response is nil and so it blows up there…

It’s easy to replicate, just try calling the action of another  
controller (that calls render\_to\_string) inside a controller.
