Graceful degrading AJAX show/hide


#1

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.


#2

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.


#3

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 wrote:

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


#4

On 5/3/06, Paul S. 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.