Forum: Ruby on Rails Accessing rails helpers when processing erb snippets

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
44a43e7fef8e933e802a7802b4bd3525?d=identicon&s=25 John Small (johnsmall)
on 2008-12-20 22:09
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 Small
44a43e7fef8e933e802a7802b4bd3525?d=identicon&s=25 John Small (johnsmall)
on 2008-12-20 22:18
John Small 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 Small
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 RichardOnRails (Guest)
on 2008-12-20 22:35
(Received via mailing list)
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 Small <rails-mailing-l...@andreas-s.net>
44a43e7fef8e933e802a7802b4bd3525?d=identicon&s=25 John Small (johnsmall)
on 2008-12-21 15:55
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 Small <rails-mailing-l...@andreas-s.net>

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 Small
31fdda5f38fc6df4e193cfb7445ddc5a?d=identicon&s=25 RichardOnRails (Guest)
on 2008-12-22 15:16
(Received via mailing list)
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 Small <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.