Topic: Redirecting to plain forms when AJAX doesnt work on IE

Hi,

I have a site that uses AJAX/JAVASCRIPT extensively, which works fine on Firefox, IE6 and IE7, but if JAVASCRIPT is turned off in an IE browser, is it possible to redirect the link of an AJAX form to a plain html form?

Please advise me on this, any help is much appreciated.

Cheers

Cass

Re: Redirecting to plain forms when AJAX doesnt work on IE

pass an :html option to set the "action" attribute of the form tag to an URL:

<% remote_form_for :bla, :url {:action => "create"},
                   :html => {:action => :url_for => { :action => "create"} } do |form| %>

:url gets the AJAX url, :html => :action gets the normal HTTP url.

But That shouldn't even be nessessary, as by default, remote form helpers set this value to the same as in :url.
So weither or not JS is enabled, you should be able to szbmit the form...

whats not working? can you show some code?

Last edited by Duplex (2007-10-18 08:08:33)

Re: Redirecting to plain forms when AJAX doesnt work on IE

Hi Duplex,

Thank you very much for the reply, I would actually like to give you the exact context. This is my ajax link:

<%= link_to_remote "Collaborations",
:update => "collabu",
:url => { :action => "collaborations" }%>
<div id="collabu">
</div>

could you please tell me how we can redirect to plain html for this information, actually here it is not a form that is being called but some text information.

I actually tried this:


<%= link_to_remote "Collaborations",
:update => "collabu",
:url => { :action => "collaborations" },
:html => {:action => :url_for => { :action => "collaborations"}} do |form| %>
<div id="collabu">
</div>

and got the following error:

SyntaxError in Login#register

Showing app/views/layouts/movies.rhtml where line #132 raised:

compile error
./script/../config/../app/views/layouts/movies.rhtml:132: syntax error, unexpected tASSOC, expecting '}'
:html => {:action => :url_for => { :action => "collaborations"}} do |form| ).to_s); _erbout.concat "\n"
                                ^
./script/../config/../app/views/layouts/movies.rhtml:132: syntax error, unexpected '}', expecting ')'
:html => {:action => :url_for => { :action => "collaborations"}} do |form| ).to_s); _erbout.concat "\n"
                                                                 ^

Extracted source (around line #132):

129: <%= link_to_remote "Collaborations",
130: :update => "collabu",
131: :url => { :action => "collaborations" },
132: :html => {:action => :url_for => { :action => "collaborations"}} do |form| %>
133: <div id="collabu">
134: </div>
135: </td>


Cheers

Cass

Last edited by myowntribe (2007-10-18 08:29:10)

Re: Redirecting to plain forms when AJAX doesnt work on IE

ah, so we are talking about a link_to_remote, not a form ... the form is the result of the action being called (and therefore not the important point)

link_to_remote should gracefully degrade when javascript is disabled, meaning that when no JS is executed, the link is called with a normal HTTP reuqest by the browser. So the view and the link_to remote sould be fine as-is.

And as you have specified an :update option in the link_to_remote, even the AJAX call expects pure HTML back, so it depends how your views are set up:

i guess, with Javascript, you want to render a form partial back to the page, while when an HTTP call is made, you want to render the action again, and in the action's view, include the form partial. Am i halfway  correct?

Now in the controller you have to use respond_to like this:

def collaborations
  # some other code
  respond_to do |format|
    format.html  {render :action => "view_your link_to_remote_is_in", :locals { :showform => true }
    format.js { render :partial => "form"}
  end
end

#in the view template "view_your link_to_remote_is_in.rhtml":
<div id="collabu">
<% if showform %>
  <%= render :partial => "form" %>
<%end%>
</div>


This is only some pseudo-code as i don't know your controller and view/partial setup exactly, hope you see where i am heading with this.

Re: Redirecting to plain forms when AJAX doesnt work on IE

Hi Duplex,

thanks for the reply, what you gave makes sense, let me try and implement it, and see, will be back...

Cheers

Cass