# If.request.xhr?

**URL:** <https://www.ruby-forum.com/t/if-request-xhr/72471>\
**Category:** Rails\
**Created:** [October 17, 2006, 1:25pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471 "2006-10-17T13:25:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 1:25pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/1 "2006-10-17T13:25:15Z")

</div>

Hope I’m not to far off base here -  
I want a link that will first go the controller, test for javascript and  
then re-direct to either ajax or non-ajax page.  
I have the controller code set up but a bit unsure about the link itself

Do I need to use a link\_to\_remote link to get the user over to the  
controller /action and if.request.xhr?  
or should just a link\_to work.  
I ask because a link\_to doesn’t seem to mean anything , js is turned on  
and  
still wind up at the non-ajax form.

Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/2 "2006-10-17T13:51:29Z")

</div>

Hi,

If your using the normal link\_to helper, your link wont use a  
xmlhttprequest-object to send the request, but a normal get/post  
request. Link\_to\_remote uses the prototype javascript library to send  
an ajax-request and you will be able to recognize the request as ajax  
in your controller.

Richard

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 1:57pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/3 "2006-10-17T13:57:31Z")

</div>

On 10/17/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Richard  
> Okay, that’s what I thought. However , the link isn’t working , meaning  
> right now when I place the mouse over the link it says /mydomain/#

I have this in the html doc:

\<%= javascript\_include\_tag "prototype" %\>

And have composed the link as such (though I’ve tried a few different  
variations)  
\<%= link\_to\_remote ‘Search’, {:url =\> { :action =\> ‘failover’ }} %\>

Shouldn’t that create the link ?

Thank you  
Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/4 "2006-10-17T17:10:50Z")

</div>

Dark A. \<sambient@…\> writes:

> Okay, that’s what I thought. However , the link isn’t working , meaning  
> right now when I place the mouse over the link it says /mydomain/#

That’s because the link is called with Javascript and coded into a JS  
function  
call, whereas the status bar only shows the value of the link’s href  
attribute.  
View Source and you’ll see an onclick which actually calls the remote  
call.

The itself never actually gets activated, it’s disabled by the  
javascript in  
the s onclick.

If you want, you can supply a URL to be called if people have javascript  
switched off, by adding an :href to the link\_to\_remote:

\<%= link\_to\_remote “Text”, {:controller =\> blah, :action =\> blah},  
{:href =\>  
url\_for(:controller =\> blah2, :action =\> blah3)}

This will allow non-javascript users to access your site and possibly  
get the  
information they would have gotten, at the cost of a full page request.  
That’s  
not an automatic thing thought, your code has to be able to deal with  
both  
requests

Gareth

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 5:04pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/5 "2006-10-17T17:04:48Z")

</div>

Bump 🙂

On 10/17/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > an ajax-request and you will be able to recognize the request as ajax  
> > \<%= javascript\_include\_tag “prototype” %\>
> 
> And have composed the link as such (though I’ve tried a few different  
> variations)  
> \<%= link\_to\_remote ‘Search’, {:url =\> { :action =\> ‘failover’ }} %\>
> 
> Shouldn’t that create the link ?

Maybe I’m way off base here. I am looking at a codecite presentation.  
I’ve set up my controller action as such:

def failover  
if request.xhr?  
redirect\_to :controller =\> ‘ajaxsearch’, :action =\> ‘index’  
else  
redirect\_to :controller =\> ‘searches’, :action =\> ‘new’

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/6 "2006-10-17T17:49:18Z")

</div>

On 10/17/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > right now when I place the mouse over the link it says /mydomain/#  
> > > the s onclick.  
> > > information they would have gotten, at the cost of a full page request.  
> > > \<%= link\_to\_remote “Search for jobs”, {:controller =\> ‘ajaxsearch’,
> 
>  
> 
> Am I totally lame and missing something or is something weird going on ?
> 
>  
> 
> Stuart

 

Alright, I think I got it now , I added the needed div / dom id. Which  
updated the page the link is on. I guess that is the way it’s intended  
to  
work.

 

Stuart

 

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/7 "2006-10-17T18:41:39Z")

</div>

Dark A. \<sambient@…\> writes:

> Alright, I think I got it now , I added the needed div / dom id. Which updated  
> the page the link is on. I guess that is the way it’s intended to work.
> 
> Stuart

That’ll be my bad, for trying to guess the link\_to\_remote function call.  
Well  
done for working it out

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 17, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/if-request-xhr/72471/8 "2006-10-17T17:16:53Z")

</div>

On 10/17/06, Gareth A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> attribute.  
> \<%= link\_to\_remote “Text”, {:controller =\> blah, :action =\> blah}, {:href  
> Gareth  
> Thanks Gareth. Unfortunately then something is going wrong here.  
> I created the link\_to\_remote -  
> \<%= link\_to\_remote “Search for jobs”, {:controller =\> ‘ajaxsearch’,  
> :action  
> =\> ‘search’}, {:href =\>  
> url\_for(:controller =\> ‘searches’, :action =\> ‘new’)}%\>

I believe that is right. With javascript turned on the link does  
nothing.  
With js off, I get taken to the right place.

Page source :  
[Search for jobs](https://www.ruby-forum.com/searches/new)

Am I totally lame and missing something or is something weird going on ?

Stuart
