# After\_filter and partials

**URL:** <https://www.ruby-forum.com/t/after-filter-and-partials/145101>\
**Category:** Rails\
**Created:** [August 6, 2008, 1:24am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101 "2008-08-06T01:24:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 1:24am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/1 "2008-08-06T01:24:14Z")

</div>

Hello !

I’d like to use a filter to add something like the following script to  
all views rendered by my application :

The goal is to update a div with some flash content, systematically,  
without needing to write code in every view or method.

I don’t know how to proceed with filters, how to access to the repsonse  
body and to change it…

If anybody could help …

thanks

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 1:29am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/2 "2008-08-06T01:29:29Z")

</div>

You dont need a filter, you need a layout

[http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html)

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 1:45am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/3 "2008-08-06T01:45:07Z")

</div>

nico Itkin wrote:

> Hello !
> 
> I’d like to use a filter to add something like the following script to  
> all views rendered by my application :
> 
> The goal is to update a div with some flash content, systematically,  
> without needing to write code in every view or method.
> 
> I don’t know how to proceed with filters, how to access to the repsonse  
> body and to change it…
> 
> If anybody could help …
> 
> thanks

In the application controller, the following code :

after\_filter :update\_notice

def update\_notice  
response.body \<\< “”  
end

render the expected code on the page :

but this js code is not evaluate…

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 1:47am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/4 "2008-08-06T01:47:27Z")

</div>

Stefan B. wrote:

> You dont need a filter, you need a layout
> 
> [http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html](http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html)

The application is ajax oriented so layouts are not refreshed on every  
action, i 'm looking for some generic tool

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 2:07am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/5 "2008-08-06T02:07:54Z")

</div>

Stefan B. wrote:

> Oh, i see… Sorry
> 
> You have to instruct prototype to evalulate the script tags of the xhr  
> response. afaik, by default, the response is only evaluated if the  
> mimetype is javascript. but I’m not sure so I suggest you to look it up  
> in the prototype api and the rails prototype helpers.
> 
> Since rails javascript helpers generate   
> instead of you could try changing that  
> too.

 works when html is rendered but not js. I've to adapt the method depending on the mime type of the response, but i'm in the dark: i can't find any doc on this do you know any paper where the response object is described ? Nico

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 1:57am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/6 "2008-08-06T01:57:41Z")

</div>

Oh, i see… Sorry

You have to instruct prototype to evalulate the script tags of the xhr  
response. afaik, by default, the response is only evaluated if the  
mimetype is javascript. but I’m not sure so I suggest you to look it up  
in the prototype api and the rails prototype helpers.

Since rails javascript helpers generate   
instead of you could try changing that  
too.

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 2:56am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/7 "2008-08-06T02:56:04Z")

</div>

nico Itkin wrote:

> Stefan B. wrote:
> 
> > Oh, i see… Sorry
> > 
> > You have to instruct prototype to evalulate the script tags of the xhr  
> > response. afaik, by default, the response is only evaluated if the  
> > mimetype is javascript. but I’m not sure so I suggest you to look it up  
> > in the prototype api and the rails prototype helpers.
> > 
> > Since rails javascript helpers generate ” unless response.headers[‘Content-Type’]  
> > == ‘text/javascript’  
> > end
> > 
> > thank you stefan !

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 2:57am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/8 "2008-08-06T02:57:47Z")

</div>

seems i was wrong. nice 🙂

---

<div class="post-metadata">

**Author:** ![itkin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@itkin](https://www.ruby-forum.com/u/itkin)\
**Post date:** [August 6, 2008, 2:57am UTC](https://www.ruby-forum.com/t/after-filter-and-partials/145101/9 "2008-08-06T02:57:01Z")

</div>

so you’re rendering an rjs template as response and want to add  
additional js code using a filter? Phew I don’t think that is possible  
without any code in the rjs.
