Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refreshing a div with the contents of flash[:notice] using right.js?

I am using inline haml templates in my sinatra app. I have a @@layout with a section like this:

#message
  - if flash[:notice]
    %section.notice= flash[:notice]
  - if flash[:error]
    %section.error= flash[:error]

When I use flash[:notice] = "Hello!" in a route & click on a link, the next page happily says Hello" in the #message div. great.

So here is the problem, I am using right.js to add some ajax niceness to my app, The behavior of rack-flash is inconsistent at best.

Most of the time, you click a link (.linkey), a little bit of javascript intercepts it & loads into a #content div (this part works as well), then the '#message' div is reloaded & the flash for the previous action is displayed... the next time you click a link... about 80% of the time, the rest of the time nothing is displayed.

This is my js:

"a.linkey".onClick(function(event) {
   event.stop();
   $('content').load( [ "/", this.get('id'), ].join("") );
   $('message').load( '/message' );
});

I would like is something like this:

1) Click link

2) the link target (/foo) is loaded into #content

3) #message is reloaded with the message (from the route flash[:notice] = "bar")

4) #content now displaying /foo & #message displays "bar"

I have also tried this with $('message').load( '/message' ); but that either loads nothing into #message or fills #message with "/message" (a string not the contents).

I would like to know what is going on here? Is it something with rack-flash or right.js? or something else? I can provide more code if needed but there really isn't much more beyond the basic framework as I am just starting on this project.

like image 885
Don Graziano Avatar asked Feb 25 '12 02:02

Don Graziano


1 Answers

For AJAX requests running through a controller action, you'd need to use flash.now so that the flash message will be available for the current request. So in your case, flash.now[:notice] = "bar".

like image 194
Carlos Ramirez III Avatar answered Oct 05 '22 03:10

Carlos Ramirez III