# Append/Prepend to a template from controller method

**URL:** <https://www.ruby-forum.com/t/append-prepend-to-a-template-from-controller-method/182617>\
**Category:** Rails\
**Created:** [January 31, 2010, 4:38am UTC](https://www.ruby-forum.com/t/append-prepend-to-a-template-from-controller-method/182617 "2010-01-31T04:38:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 31, 2010, 4:38am UTC](https://www.ruby-forum.com/t/append-prepend-to-a-template-from-controller-method/182617/1 "2010-01-31T04: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)  
@redirect\_delay = delay  
@redirect\_url = url  
render  
end

app/views/layouts/application.html.erb

 \<%= yield :refresh\_tag %\> \<%= yield %\> 

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  
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 %\>  
  
\<% end %\>

# Please wait while you are redirected...

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:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 31, 2010, 7:54am UTC](https://www.ruby-forum.com/t/append-prepend-to-a-template-from-controller-method/182617/2 "2010-01-31T07:54:49Z")

</div>

Hello–

On Jan 30, 2010, at 7:38 PM, Aaron Stacy wrote:

> content=‘\<%=@redirect\_delay%\>;url=\<%=@redirect\_url%\>’\>  
> \<% end %\>
> 
> # Please wait while you are redirected...
> 
> 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.

Check out after filters:  
[http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html](http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html)

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 31, 2010, 8:08am UTC](https://www.ruby-forum.com/t/append-prepend-to-a-template-from-controller-method/182617/3 "2010-01-31T08: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,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ktheory](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@ktheory](https://www.ruby-forum.com/u/ktheory)\
**Post date:** [January 31, 2010, 11:42pm UTC](https://www.ruby-forum.com/t/append-prepend-to-a-template-from-controller-method/182617/4 "2010-01-31T23: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.
