# About filter

**URL:** <https://www.ruby-forum.com/t/about-filter/145513>\
**Category:** Rails\
**Created:** [August 11, 2008, 4:45am UTC](https://www.ruby-forum.com/t/about-filter/145513 "2008-08-11T04:45:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 11, 2008, 4:45am UTC](https://www.ruby-forum.com/t/about-filter/145513/1 "2008-08-11T04:45:59Z")

</div>

Hello:  
I have a filter mehtod defined in the application.rb for  
before\_filter,and a method(create\_action invoked in thhe ajax link)using  
the filter.When authorize’s redirect\_to happens and the create\_action’s  
codes don’t execute,the login template is inserted to the dom element  
whose id is side.But i don’t want that result.In this case i want the  
browser to redirect to the login action rendering in the login layout  
instead of a remote insert.I want filter to work like this for the  
remote link while still working unchangeably for many normal link. How  
do i modify it?  
###############################  
def authorize  
unless session[:login] && User.find\_by\_id(session[:login])  
flash[:login]=“æ‚¨è¿˜æ²¡ç™»å½•æˆ–æ³¨å†Œä¸ºç”¨æˆ·”  
redirect\_to(:controller=\>“login”,:action=\>“login”)  
end  
end  
##########################  
\<%form\_remote\_tag(:update=\>“side”,:url=\>{:action=\>“create\_action”},:method=\>:post)do-%\>

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 11, 2008, 6:58am UTC](https://www.ruby-forum.com/t/about-filter/145513/2 "2008-08-11T06:58:23Z")

</div>

not use the ajax to resolve it?  
i need your help!

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 11, 2008, 10:51am UTC](https://www.ruby-forum.com/t/about-filter/145513/3 "2008-08-11T10:51:58Z")

</div>

On Aug 11, 5:58 am, Guo Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> not use the ajax to resolve it?  
> i need your help!

As you’ve found, if you do a redirect then the redirected content is  
inserted into the DOM (the javascript handling the ajax update never  
sees the redirect. if you were using rjs (ie if your form\_remode\_tag  
didn’t have the :update option) then you could do an rjs update, but  
you’re not, so that won’t work either.  
If you don’t want to change the form\_remote\_tag, one way out is to  
have the ajax call’s onComplete check for a certain header field, and  
if it is present, redirect (client side) to the login page. In your  
filter if you detect that the request is an ajax one then instead of  
redirecting them to the login page just set that magic header and  
render nothing.

Fred

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 11, 2008, 11:23am UTC](https://www.ruby-forum.com/t/about-filter/145513/4 "2008-08-11T11:23:16Z")

</div>

if you were using rjs (ie if your form\_remode\_tag

> didn’t have the :update option) then you could do an rjs update, but  
> you’re not, so that won’t work either.

you mean that if i use rjs update,i can manage it? but how ? how to keep  
the ajax and redirect to exist for the filter?

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 11, 2008, 11:36am UTC](https://www.ruby-forum.com/t/about-filter/145513/5 "2008-08-11T11:36:14Z")

</div>

thank you!

---

<div class="post-metadata">

**Author:** ![daociyiyou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/0ea827/32.png) [@daociyiyou](https://www.ruby-forum.com/u/daociyiyou)\
**Post date:** [August 11, 2008, 11:32am UTC](https://www.ruby-forum.com/t/about-filter/145513/6 "2008-08-11T11:32:56Z")

</div>

On 11 Aug 2008, at 10:23, Guo Y. wrote:

> if you were using rjs (ie if your form\_remode\_tag
> 
> > didn’t have the :update option) then you could do an rjs update, but  
> > you’re not, so that won’t work either.
> 
> you mean that if i use rjs update,i can manage it? but how ? how to  
> keep  
> the ajax and redirect to exist for the filter?

If you use rjs you can just do page.redirect\_to(…) but as I said  
that won’t work with your existing setup because your ajax request is  
expecting html back, not javascript.

Fred
