rendering a view outside of controller ?

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..

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.

Hemant Kumar 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

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.

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.

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/<path>/<filename>", "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