# Distinguish between Ajax.Request and Updater?

**URL:** <https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351>\
**Category:** Rails\
**Created:** [November 27, 2006, 9:34am UTC](https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351 "2006-11-27T09:34:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [November 27, 2006, 9:34am UTC](https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351/1 "2006-11-27T09:34:56Z")

</div>

Is there a way to tell if an actiojn was called with Ajax.Request as  
opposed to Ajax.Updater? Ie., if link\_to\_remote was called without the  
:update param, and with the :update param?

In my action I would like to know if I should use render :update or I  
can just use render for this particular request. For performance  
reasons, I want to use :update where possible, since it is twice as fast  
without the escaping, but there are still times I need my action to do  
more than just render a div…

Obviously I can do it with some extra param in the link\_to\_remote :url,  
but if there is some ‘well known’ rails way of doing this I would like  
to know.

Thanks.

---

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [November 27, 2006, 10:29am UTC](https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351/2 "2006-11-27T10:29:49Z")

</div>

I believe this is what respond\_to is for:

respond\_to do |wants|  
wants.html {#render some html}  
wants.js do {#render some rjs}  
end

Fred

---

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [November 28, 2006, 4:16am UTC](https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351/3 "2006-11-28T04:16:32Z")

</div>

Fred wrote:

> I believe this is what respond\_to is for:
> 
> respond\_to do |wants|  
> wants.html {#render some html}  
> wants.js do {#render some rjs}  
> end
> 
> Fred

Ah, thanks… knew it was something simple…just couldnt put my finger  
on it.

---

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [December 1, 2006, 11:39am UTC](https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351/4 "2006-12-01T11:39:27Z")

</div>

Slain W. wrote:

> Fred wrote:
> 
> > I believe this is what respond\_to is for:
> > 
> > respond\_to do |wants|  
> > wants.html {#render some html}  
> > wants.js do {#render some rjs}  
> > end
> > 
> > Fred

Actually, this doesn’t work. link\_to\_remote :update =\> ‘some\_div’  
triggers the wants.js even though it really only wants.html by  
definition.

There is a problem in prototype 1.5.0\_rc0 in setRequestHeaders causing  
this:

‘Accept’, ‘text/javascript, text/html, application/xml, text/xml,  
_/_’];

will ALWAYS trigger the wants.js in a link\_to\_remote :update and never  
the wants.html. The request headers for an Ajax.Request and an  
Ajax.Updater should be different…  
Update should be:  
‘Accept’, ‘text/html, text/javascript, application/xml, text/xml,  
_/_’];

and request should be

‘Accept’, ‘text/javascript, application/xml, text/xml, _/_’];

I’m thinking. Or else, rails needs another way of knowing whether html  
or js is wanted…

Adding a custom setRequestHeaders to Ajax.Updater that omits the  
javascript option works as well…

setRequestHeaders: function() {  
var requestHeaders =  
[‘X-Requested-With’, ‘XMLHttpRequest’,  
‘X-Prototype-Version’, Prototype.Version,  
‘Accept’, ‘text/html, application/xml, text/xml, _/_’];

```
if (this.options.method == 'post') {
  requestHeaders.push('Content-type', this.options.contentType);

  /* Force "Connection: close" for Mozilla browsers to work around
   * a bug where XMLHttpReqeuest sends an incorrect Content-length
   * header. See Mozilla Bugzilla #246651.
   */
  if (this.transport.overrideMimeType)
    requestHeaders.push('Connection', 'close');
}

if (this.options.requestHeaders)
  requestHeaders.push.apply(requestHeaders, 

```

this.options.requestHeaders);

```
for (var i = 0; i < requestHeaders.length; i += 2)
  this.transport.setRequestHeader(requestHeaders[i], 

```

requestHeaders[i+1]);  
},

---

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [December 1, 2006, 2:31pm UTC](https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351/5 "2006-12-01T14:31:42Z")

</div>

Alan F. wrote:

> Slain W. wrote:
> 
> > Slain W. wrote:
> > 
> > > Fred wrote:
> > > 
> > > > I believe this is what respond\_to is for:
> > > > 
> > > > respond\_to do |wants|  
> > > > wants.html {#render some html}  
> > > > wants.js do {#render some rjs}  
> > > > end
> > > > 
> > > > Fred
> > 
> > Actually, this doesn’t work. link\_to\_remote :update =\> ‘some\_div’  
> > triggers the wants.js even though it really only wants.html by  
> > definition.
> 
> Not the solution you’re looking for, I know, but can you go for two  
> separate actions with the common code extracted ?
> 
> A.

The patch to prototype works, so I’m happy. And no I dont really want  
to break that out in a dozen actions 😛 But it would work of course, or  
as I mentioned, adding another parameter to the link\_to\_remote :url like  
:is\_update =\> true

---

<div class="post-metadata">

**Author:** ![slain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/2acd7d/32.png) [@slain](https://www.ruby-forum.com/u/slain)\
**Post date:** [December 1, 2006, 1:31pm UTC](https://www.ruby-forum.com/t/distinguish-between-ajax-request-and-updater/77351/6 "2006-12-01T13:31:52Z")

</div>

Slain W. wrote:

> Slain W. wrote:
> 
> > Fred wrote:
> > 
> > > I believe this is what respond\_to is for:
> > > 
> > > respond\_to do |wants|  
> > > wants.html {#render some html}  
> > > wants.js do {#render some rjs}  
> > > end
> > > 
> > > Fred
> 
> Actually, this doesn’t work. link\_to\_remote :update =\> ‘some\_div’  
> triggers the wants.js even though it really only wants.html by  
> definition.

Not the solution you’re looking for, I know, but can you go for two  
separate actions with the common code extracted ?

A.
