# Append/Prepend to a template from controller method

**URL:** <https://discuss.rubyonrails.org/t/append-prepend-to-a-template-from-controller-method/49001>\
**Category:** rubyonrails-talk\
**Created:** [January 31, 2010, 3:38am UTC](https://discuss.rubyonrails.org/t/append-prepend-to-a-template-from-controller-method/49001 "2010-01-31T03:38:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 31, 2010, 3:38am UTC](https://discuss.rubyonrails.org/t/append-prepend-to-a-template-from-controller-method/49001/1 "2010-01-31T03:38:17Z")

</div>

I want a method in app/controllers/application.rb that can prepend/append text to whatever template gets rendered. Of course I can't call render twice w/o getting a double render error, so is this possible?

I want to redirect after a delay using a meta refresh. Here's what I've got:

app/controllers/application\_controller.rb:

def redirect\_after\_delay (url, delay) &nbsp;&nbsp;@redirect\_delay = delay &nbsp;&nbsp;@redirect\_url = url &nbsp;&nbsp;render end

app/views/layouts/application.html.erb

\<!DOCTYPE html\> \<html lang="en"\> &nbsp;&nbsp;\<head\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= yield :refresh\_tag %\> &nbsp;&nbsp;\</head\>

&nbsp;&nbsp;\<body\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= yield %\> &nbsp;&nbsp;\</body\> \</html\>

So then if I want to add a redirect-after-delay, I add the following to 1) my controller and 2) the action's view:

app/controllers/my\_controller.rb

def my\_action &nbsp;&nbsp;redirect\_after\_delay '[http://www.google.com](http://www.google.com)', 3 if some\_condition end

app/views/my\_controller/my\_action.html.erb

\<% content\_for :refresh\_tag do %\> &nbsp;&nbsp;\<meta http-equiv='refresh' content='\<%=@redirect\_delay%\>;url=\<%=@redirect\_url%\>'\> \<% end %\> \<h1\>Please wait while you are redirected...\</h1\>

Since the content\_for block never changes, would be nice to just call something from the redirect\_after\_delay method to add it to whatever template is going to be rendered.

---

<div class="post-metadata">

**Author:** ![Steve\_Ross](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Steve\_Ross](https://discuss.rubyonrails.org/u/Steve_Ross)\
**Post date:** [January 31, 2010, 6:53am UTC](https://discuss.rubyonrails.org/t/append-prepend-to-a-template-from-controller-method/49001/2 "2010-01-31T06:53:18Z")

</div>

Hello--

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 31, 2010, 7:08am UTC](https://discuss.rubyonrails.org/t/append-prepend-to-a-template-from-controller-method/49001/3 "2010-01-31T07:08:56Z")

</div>

Aaron Stacy wrote:

> I want a method in app/controllers/application.rb that can prepend/append text to whatever template gets rendered.

[...]

This is probably best done in the application layout, or else in a before\_filter.

Best,

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [January 31, 2010, 10:42pm UTC](https://discuss.rubyonrails.org/t/append-prepend-to-a-template-from-controller-method/49001/4 "2010-01-31T22:42:05Z")

</div>

Marnen Laibow-Koser wrote:

> This is probably best done in the application layout, or else in a before\_filter.

I think you're right, I may have been trying to make it too complicated. Thanks for the suggestions.
