Accessing rails helpers when processing erb snippets

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.

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.

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. [email protected]

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. [email protected]

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.

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. [email protected]