# Accessing rails helpers when processing erb snippets

**URL:** <https://www.ruby-forum.com/t/accessing-rails-helpers-when-processing-erb-snippets/156117>\
**Category:** Rails\
**Created:** [December 20, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/accessing-rails-helpers-when-processing-erb-snippets/156117 "2008-12-20T22:09:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 20, 2008, 10:09pm UTC](https://www.ruby-forum.com/t/accessing-rails-helpers-when-processing-erb-snippets/156117/1 "2008-12-20T22:09:03Z")

</div>

I need to be able to merge a lot of content for web pages, along with  
links to other pages taken from a list of categories. The text that goes  
with each category can be different depending on the context that it’s  
displayed in.

I could do this by having a whole stack of \*.html.erb files which hold  
the static content and access the Rails helpers to pull in stuff from  
the database and build the links & etc. But that’s a rubbish way to do  
things because I’d end up with too many \*.erb files to manage. I want to  
keep these little snippets of erb or haml ready texts in the DB where  
they belong, and pull them out and process them into html when required.  
But… how do I get access to the rails helpers when I’m inside these  
little snippets.

Basically I need to call render, without the layout, and then grab the  
result and put that into the final page. Sort of like a partial, expect  
that it’s not something that can be generalised. But I don’t want render  
to then disappear off and hand the result back to the caller I want  
render to give me the result and then I’ll merge it with other bits and  
then hand the completed page to the caller.

Any thoughts?

Ta

John S.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 20, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/accessing-rails-helpers-when-processing-erb-snippets/156117/2 "2008-12-20T22:18:44Z")

</div>

John S. wrote:

> I need to be able to merge a lot of content for web pages, along with  
> links to other pages taken from a list of categories. The text that goes  
> with each category can be different depending on the context that it’s  
> displayed in.
> 
> I could do this by having a whole stack of \*.html.erb files which hold  
> the static content and access the Rails helpers to pull in stuff from  
> the database and build the links & etc. But that’s a rubbish way to do  
> things because I’d end up with too many \*.erb files to manage. I want to  
> keep these little snippets of erb or haml ready texts in the DB where  
> they belong, and pull them out and process them into html when required.  
> But… how do I get access to the rails helpers when I’m inside these  
> little snippets.
> 
> Basically I need to call render, without the layout, and then grab the  
> result and put that into the final page. Sort of like a partial, expect  
> that it’s not something that can be generalised. But I don’t want render  
> to then disappear off and hand the result back to the caller I want  
> render to give me the result and then I’ll merge it with other bits and  
> then hand the completed page to the caller.
> 
> Any thoughts?
> 
> Ta

Ha, haaa. Having written out the question made me clearly formulate in  
my mind what I wanted to do. So now I’ve been able to answer my own  
question.

I just call render\_to\_string with an inline template. Simple.

> John S.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 20, 2008, 10:35pm UTC](https://www.ruby-forum.com/t/accessing-rails-helpers-when-processing-erb-snippets/156117/3 "2008-12-20T22:35:47Z")

</div>

Hi John,

Congrats on figuring that out. It sounds like a useful construct for  
a number of situations. Did you make a small test implementation? If  
so, could you post a couple of callers and renderers to help newbies  
see actual code?

Thanks in advance,  
Richard

On Dec 20, 4:18 pm, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 21, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/accessing-rails-helpers-when-processing-erb-snippets/156117/4 "2008-12-21T15:55:58Z")

</div>

RichardOnRails wrote:

> Hi John,
> 
> Congrats on figuring that out. It sounds like a useful construct for  
> a number of situations. Did you make a small test implementation? If  
> so, could you post a couple of callers and renderers to help newbies  
> see actual code?
> 
> Thanks in advance,  
> Richard
> 
> On Dec 20, 4:18ï¿½pm, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Well it turned out to be not quite so simple. “render\_to\_string” isn’t  
available inside helpers or views, which is where I wanted to use it.  
But after a while scratching around I realised that inside a view or a  
helper “render” does exactly what “render\_to\_string” does inside an  
ActionController.

So I just needed to call render :inline=\>MySpecialErbString

But I couldn’t get a “\<%= yield %\>” inside the inline text to yield to a  
block. That’s a different problem which I’ve got round another way. If  
I’ve got a moment I’ll come back and look at why I can’t yield inside a  
call to ActionView#render.

So the code is basically

\<%= render :inline=\>my\_model.A\_field\_that\_stores\_erb\_text %\> and so on.

John S.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 22, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/accessing-rails-helpers-when-processing-erb-snippets/156117/5 "2008-12-22T15:16:39Z")

</div>

Thanks for the additional insight. I’ll try to build an example for  
my own edification.

Best wishes,  
Richard

On Dec 21, 9:55 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
