Forum: Ruby on Rails Graceful degrading AJAX show/hide

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul S. (Guest)
on 2006-05-03 21:06
(Received via mailing list)
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.

--
Juan Lupión (Guest)
on 2006-05-03 21:06
(Received via mailing list)
> 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.


--
Paul S. (Guest)
on 2006-05-03 21:07
(Received via mailing list)
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
>

--
Juan Lupión (Guest)
on 2006-05-03 21:07
(Received via mailing list)
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.



--
This topic is locked and can not be replied to.