# Calling render\_to\_string from a separate controller

**URL:** <https://www.ruby-forum.com/t/calling-render-to-string-from-a-separate-controller/122174>\
**Category:** Rails\
**Created:** [December 5, 2007, 6:49am UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-separate-controller/122174 "2007-12-05T06:49:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![squ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@squ](https://www.ruby-forum.com/u/squ)\
**Post date:** [December 5, 2007, 6:49am UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-separate-controller/122174/1 "2007-12-05T06:49:54Z")

</div>

Hello everyone. I’m having some trouble with calling render\_to\_string  
from a separate controller. I’m also new with using methods from other  
controllers, but I think I’ve got a grasp on that.

So here’s what I’m doing:

- In controller A:  
blah = ControllerB.new  
blah.doStuff()

- In controller B  
def doStuff  
…  
mystring = render\_to\_string :partial =\> ‘blahblah’  
end

And this is the error I’m receiving:  
" NoMethodError in ThingsController#create

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.body

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/base.rb:908:in  
`erase_render_results' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/base.rb:788:in `render\_to\_string’  
#{RAILS\_ROOT}/app/controllers/facebook\_controller.rb:43:in  
`update_profile' #{RAILS_ROOT}/app/controllers/things_controller.rb:43:in `create’  
"

The problem is with the render\_to\_string being called. I did some  
research into erase\_render\_results, which is called by render\_to\_string  
to apparently clear the output, and prevent DoubleRender errors. You can  
see the source (I think) here: [http://dev.rubyonrails.org/changeset/1918](http://dev.rubyonrails.org/changeset/1918)

So, I believe it’s breaking on the line “@response.body = nil”

Does anybody have any advice about how to use render\_to\_string in a  
controller, being called from a different controller? It’s driving me  
crazy, and is starting to seem like a Ruby bug. The code runs fine if  
called directly from controller B, but several different controllers  
will need to call this method, and I’d like to keep it out of  
application.rb if possible.

---

<div class="post-metadata">

**Author:** ![squ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/74df32/32.png) [@squ](https://www.ruby-forum.com/u/squ)\
**Post date:** [December 15, 2007, 9:18pm UTC](https://www.ruby-forum.com/t/calling-render-to-string-from-a-separate-controller/122174/2 "2007-12-15T21:18:18Z")

</div>

Jared,

Any luck with this? I’m running into the same issue.

Ben

On Dec 5, 12:49 am, Jared S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
