# Using rjs after a redirect

**URL:** <https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941>\
**Category:** Rails\
**Created:** [August 4, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941 "2008-08-04T15:45:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 4, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941/1 "2008-08-04T15:45:30Z")

</div>

Let’s say I have an app where a user creates something, e.g. maybe they  
create a new recipe, and the app redirects to the :show action of the  
recipes controller after a successful post to the :create action. How  
would I have a flash[:notice] = “Recipe successfully created.” shown  
using rjs effects on the :show action? Essentially, I’m trying to find a  
way to way to insert the following behavior after I’ve redirected to the  
:show action;

page.insert\_html :after, “all”, “#{flash[:notice]}”  
page.delay(2) do  
page.visual\_effect :fade, “notifier”  
end  
flash.discard

It’s easy to do the above when we don’t need to redirect - we just use  
the some\_action.js.rjs (or whatever the template name corresponding to  
the controller action), but I just can’t work out how to do this with a  
redirect in between the user making a request and the rjs doing its  
thing. Is this possible?

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 4, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941/2 "2008-08-04T16:19:57Z")

</div>

Hi Neil,

Neil C. wrote:

> Essentially, I’m trying to find a way to way to insert  
> the following behavior after I’ve redirected to the  
> :show action;

I’m not sure I understand the scenario completely. You can do a  
redirect on  
an XHR with RJS using page.redirect. That doesn’t work for you?

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 4, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941/3 "2008-08-04T16:22:09Z")

</div>

You could embed the rjs inline in the layout, or use a simple included  
js file.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 4, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941/4 "2008-08-04T16:56:15Z")

</div>

Bill W. wrote:

> Hi Neil,
> 
> Neil C. wrote:
> 
> > Essentially, I’m trying to find a way to way to insert  
> > the following behavior after I’ve redirected to the  
> > :show action;
> 
> I’m not sure I understand the scenario completely. You can do a  
> redirect on  
> an XHR with RJS using page.redirect. That doesn’t work for you?
> 
> Best regards,  
> Bill

Hi Bill,

The scenario is that a user has just created something (I’ll stick with  
the recipe example), and they are redirected to the show action for the  
recipe they just created. The flash[:notice] needs to be inserted via js  
and then faded out after a few seconds. I imagine it would read  
something like this, if I knew how to do it inline;

# GET /recipes/1

# GET /recipes/1.xml

def show  
@recipe = Recipe.find(params[:id])

respond\_to do |format|  
format.html { do |page|  
page.insert\_html :after, “all”, “

#{flash[:notice]}
”  
page.delay(2) do  
page.visual\_effect :fade, “notifier”  
end  
flash.discard  
}  
format.xml { render :xml =\> @recipe }  
end  
end

I didn’t know about the page.redirect method. Does that mean I could do  
something like this (in the create.js.rjs) ;

page.redirect recipe\_path(@recipe)  
page.insert\_html :after, “all”, “#{flash[:notice]}”  
etc…

Thanks Stefan, are you suggesting I could always have the rjs for the  
#notifier div set in the application.html.erb, rather than setting it  
every time in the different rjs templates? I do want this behaviour to  
be the default for the flash[:notice], so that would make sense.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 4, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941/5 "2008-08-04T17:10:13Z")

</div>

Neil C. wrote:

> Thanks Stefan, are you suggesting I could always have the rjs for the  
> #notifier div set in the application.html.erb, rather than setting it  
> every time in the different rjs templates? I do want this behaviour to  
> be the default for the flash[:notice], so that would make sense.

Yeah. Or put it directly in your application.js, that would be less  
obstrusive and keeps traffic low since the js is locally cached. Check  
in application.js if there is a div#flash element in the page, and if  
yes, do the effect.

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 4, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941/6 "2008-08-04T17:13:20Z")

</div>

Neil C. wrote:

> page.redirect recipe\_path(@recipe)  
> page.insert\_html :after, “all”, “#{flash[:notice]}”  
> etc…

No, the js will not continue after a redirect

---

<div class="post-metadata">

**Author:** ![chunkyslink](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f0a364/32.png) [@chunkyslink](https://www.ruby-forum.com/u/chunkyslink)\
**Post date:** [August 4, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/using-rjs-after-a-redirect/144941/7 "2008-08-04T20:10:42Z")

</div>

Stefan B. wrote:

> Neil C. wrote:
> 
> > page.redirect recipe\_path(@recipe)  
> > page.insert\_html :after, “all”, “
> > 
> > #{flash[:notice]}
> > ”  
> > etc…
> 
> No, the js will not continue after a redirect

Thanks again, I got it working. I’ve always had a conditional div for  
the flash notice in the application.html.erb (sat in a sidebar area  
originally), but I just made a move to a style which uses z-index to  
always sit on the bottom right of the browser chrome (a bit like the way  
facebook do their IM chat). I’ve moved the flash conditional to the  
bottom of the application.html.erb (which is where it has to go to get  
the right effect, and I’ve cobbled together something in application.js  
which seems to do the trick on the visual effects;

Event.observe(window, ‘load’,  
var notifier = $(‘notifier’)  
if(notifier) {  
new Effect.Highlight(‘notifier’, { startcolor: ‘#ffff99’, endcolor:  
‘#fffffff’ }); return false;  
}

}  
);

If anyone is interested, this is the tutorial I’ve used for the  
position:fixed on the notifier div;

> **[Welcome divinentd.com - BlueHost.com](http://divinentd.com/experiments/emulating-position-fixed.html)**
>
> Bluehost - Top rated web hosting provider - Free 1 click installs For blogs, shopping carts, and more. Get a free domain name, real NON-outsourced 24/7 support, and superior speed. web hosting provider php hosting cheap web hosting, Web hosting,...

I’ve yet to load it up on Parallels (do CSS on Mac + FF and then fix for  
the rest), but I’m sure it’ll work without too much fuss.
