after_filter and partials

Hello !

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

<script language="javascript"> $('notice').innerHTML = "#{flash[:notice]}" </script>

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

You dont need a filter, you need a layout

http://api.rubyonrails.org/classes/ActionController/Layout/ClassMethods.html

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 :

<script language="javascript"> $('notice').innerHTML = "#{flash[:notice]}" </script>

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 << "<script language='javascript'>$('notice').innerHTML = ' #{flash[:notice]} '</script>"   end

render the expected code on the page :

<script language='javascript'>$('notice').innerHTML = ' bonjour! '</script>

but this js code is not evaluate...

Stefan Buhr wrote:

You dont need a filter, you need a layout

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

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 <script type="text/javascript"> instead of <script language="javascript"> you could try changing that too.

Stefan Buhr 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 <script type="text/javascript"> instead of <script language="javascript"> you could try changing that too.

<script type="text/javascript"> 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

nico Itkin wrote:

Stefan Buhr 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 <script type="text/javascript"> instead of <script language="javascript"> you could try changing that too.

<script type="text/javascript"> 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

Solution :

def update_notice    response.body << "<script type='text/javascript'>" unless response.headers['Content-Type'] == 'text/javascript'    response.body << "\n $('notice').update('#{flash[:notice]}'); "    response.body << "</script>" unless response.headers['Content-Type'] == 'text/javascript'   end

thank you stefan !

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.

seems i was wrong. nice :slight_smile: