# Graceful degrading AJAX show/hide

**URL:** <https://www.ruby-forum.com/t/graceful-degrading-ajax-show-hide/59443>\
**Category:** Rails\
**Created:** [May 3, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/graceful-degrading-ajax-show-hide/59443 "2006-05-03T19:06:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![azgar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@azgar](https://www.ruby-forum.com/u/azgar)\
**Post date:** [May 3, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/graceful-degrading-ajax-show-hide/59443/1 "2006-05-03T19:06:49Z")

</div>

Ok i’ve done a bit of hunting around and I can’t find much  
documentation on how ensure an ajax call through an rjs file degrades  
with javascript off.

Essentially, if javascript is off, I want to refresh the page and pass  
a variable from the controller to the page the user is on. Not use the  
rjs file.

At the moment i have this:

\<%= link\_to\_remote(“Advanced Search”,

```
                     {:url => { :controller => "base",
                     :action => "advSearchAJAX" }},

                     {:href => url_for( :controller => "base",
                     :action => "advanced_search" )}

                     )%>

```

Essentially it just show/hides an advanced search if the user has  
javascript on. The :href is used when javascript is off and sends the  
user to a new page.

However, this isn’t ideal as i’d like use just one controller and to  
not change the page the user is on, just refresh it with the advanced  
search now displaying.

I had thought I could pass through a variable from the controller that  
could be used to display the advanced search. like so:

```
    <div id="adv_search"
<% unless @display == "show" -%>
   style="display: none">
<% end -%>
   <%= render :partial => "shared/layout/advanced_search" %>
</div>

```

I believe in my controller I can use

if !@request.xhr?

to check if it’s an ajax request. The problem I have is how to refresh  
the page the user is on and not use the rjs file.

Any help, much appreciated.  
Paul.

–

---

<div class="post-metadata">

**Author:** ![azgar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@azgar](https://www.ruby-forum.com/u/azgar)\
**Post date:** [May 3, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/graceful-degrading-ajax-show-hide/59443/2 "2006-05-03T19:06:55Z")

</div>

> to check if it’s an ajax request. The problem I have is how to refresh  
> the page the user is on and not use the rjs file.

I’m using something like this in my controller:

respond\_to do |type|  
type.html {render :partial =\> “mypartial”, :layout =\> “mylayout” }  
type.js {}  
end  
end

So if I’m hitting this action from a link\_to\_remote with a  
javascript-less browser, it will  
render the partial name “mypartial”. The default is to assume an AJAX  
call and will render  
the .rjs template.

–

---

<div class="post-metadata">

**Author:** ![azgar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@azgar](https://www.ruby-forum.com/u/azgar)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/graceful-degrading-ajax-show-hide/59443/3 "2006-05-03T19:07:06Z")

</div>

Thanks for that Juan. respond\_to looks useful. However, if I  
understand your example correctly, it doesn’t refresh the existing  
page the user is on. Rather, it renders a new page with just the  
partial on it. Please correct me if I am wrong though!

The show/hide I have can be called from any page (its in my  
application layout) so I need the user to remain on the same page, but  
refresh it to display the advanced search.

On 5/3/06, Juan Lupión [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> respond\_to do |type|  
> type.html {render :partial =\> “mypartial”, :layout =\> “mylayout” }  
> type.js {}  
> end  
> end

–

---

<div class="post-metadata">

**Author:** ![azgar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@azgar](https://www.ruby-forum.com/u/azgar)\
**Post date:** [May 3, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/graceful-degrading-ajax-show-hide/59443/4 "2006-05-03T19:07:09Z")

</div>

On 5/3/06, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for that Juan. respond\_to looks useful. However, if I  
> understand your example correctly, it doesn’t refresh the existing  
> page the user is on. Rather, it renders a new page with just the  
> partial on it. Please correct me if I am wrong though!

It will render the whole page if you are hitting the “non-javascript”  
part of the link\_to\_remote (i.e.: you have javascript disabled) In  
the other case (type.js is true), a .rjs template is rendered, which  
has the usual  
page[].replace\_html stuff.

–
