RJS template dont-know-how

Hello!

Get a problem out here. I need to update flash notice with every one
remote request. Flash.now[:notice] does that trick but not for
remoting methods. So I've applied some after_filter (say
clear_notice_on_xhr) but its render method not evaluating or i can't
see the result:
  def clear_notice_on_xhr
    return unless request.xhr?
    render :update do |page|
      page.replace_html("notice" , :partial => "layouts/notice")
    end
    rescue
      nil
  end

Help!

If I were you I'd remove that rescue nil statement as that will swallow any errors that might be telling you what is wrong.

Fred

ActionController::DoubleRenderError (Can only render or redirect once
per action) raises. So, thats the problem - I cant "inject" RJS into
template.

Anyone?

Anyone?

You;ve already found the problem: you've already rendered, so you
can't render again. If people could see the whole story rather than
just one bit, someone might be able to suggest an alternative.

Fred.

I found the problem but i dont know how to solve it.
Story:
I want to evaluate the same page.replace_html method in every rjs
template.
Question:
How to do that?

I think it does not work to implement it as 'after_filter' as long the
action executed before that filter calls 'render', too.

so for example:

def index
    ...
   # calls (implecitely) render index.erb.html # or in your case
another rjs
end

def clear_notice_on_xhr
    return unless request.xhr?
    # CALLS RENDER AGAIN ON THE SAME HTTP REQUEST
    # that doesnt work 'cause IMHO rails does not know how to merge
these 2 render outputs
    render :update do |page|
      page.replace_html("notice" , :partial => "layouts/notice")
    end
    rescue
      nil
  end

mercy
blog.odeley.com

Ok. I know from the beginning that DoubleRenderError, but how to
include that same page.replace_html method in every rjs
template? Rails seemed to be dynamic and powerful but in fact i cant
implement that very simple scenario... silly.

sinm wrote:

Ok. I know from the beginning that DoubleRenderError, but how to
include that same page.replace_html method in every rjs
template? Rails seemed to be dynamic and powerful but in fact i cant
implement that very simple scenario... silly.

Rather than use a partial, create the text in your after_filter method
and add a call to the end of the response body to action it as
javascript. This is a little messy, but you could hide it away. There
may well be an easier way, but this seems to work for me:

after_filter :clear_notice_on_xhr

def clear_notice_on_xhr
  new_text = "New flash notice created here"
  script = <<EOS
<script type="text/javascript">
//<![CDATA[
$('notice').update('#{new_text}')
//]]>
</script>
EOS
  response.body << script
end

This assumes you are including the Prototype javascript library in your
pages (will need re-writing otherwise) and the tag where you display
your flash has an id of "notice".

Thanx Mark for your try!
Really messy but it really works.
Finally i ended up with slightly different version.

  after_filter :clear_notice_on_xhr

  #TODO: bad smell
  def clear_notice_on_xhr
    return unless request.xhr?
    response.body = "$('notice').update('');" + response.body
    rescue
      nil #optional
  end