Topic: form_remote_tag not submitting using XMLHttpRequest

I have a edit.rhtml that uses form_remote_tag

<h1><%= @title %></h1>

<div id="show_item">

</div>

<%= form_remote_tag :url => { :action => :update, :id => @am_server},
             :update => "show_item",
             :complete => visual_effect(:highlight, "show_item") %>

  <%= render :partial => 'form' %>
<%= render :partial => 'relation' %>
  <%= submit_tag 'Save Changes' %>
<%= end_form_tag %>

<%= link_to 'Show', :action => 'show', :id => @am_application, :app_name => params[:app_name], :maintained_by => params[:maintained_by], :sort_name => params[:sort_name] %> |
<%= link_to 'Back', :action => 'list', :app_name => params[:app_name], :maintained_by => params[:maintained_by], :sort_name => params[:sort_name]%>


and when I check for request.xml_http_request? in my controller I get false value .

Here is the relevant controller portion of the code

 def edit
  @title = 'Application Modification '
    @am_application = AmApplication.find(params[:id])
    find_all_relations
  end

  def update
  @am_application = AmApplication.find(params[:id])
  set_relations
  if @am_application.update_attributes(params[:am_application])
  if request.xhr?
        render :action => 'new_result', :layout => false
      else
        render_text request.xhr?
      end

      #  flash[:notice] = 'AmApplication was successfully updated.'
      # redirect_to :action => 'show', :id => @am_application
     else
       render :action => 'edit'
     end
     
  end


Will anyone please let me know how to resolve the issue.
Thanks

Re: form_remote_tag not submitting using XMLHttpRequest

Does any one have any idea? I am unable to resolve the issue.
Please help.
Thanks

Re: form_remote_tag not submitting using XMLHttpRequest

Paging Ryanb....

Any suggestions Ryanb...
Thanks

Re: form_remote_tag not submitting using XMLHttpRequest

What version of Rails are you using?

I only ask because some of your code looks a bit dated.

Re: form_remote_tag not submitting using XMLHttpRequest

I am using rails version 1.1.6 and ruby version 1.8.5.

Can you also please specify what is in the code you found to be outdated so that I can decide if it warrants an upgrade.
Thanks

Re: form_remote_tag not submitting using XMLHttpRequest

do you have

<%= javascript_include_tag :defaults %>

in your layout or somewhere on that page?



--jake



pimpmaster wrote:

What version of Rails are you using?

I only ask because some of your code looks a bit dated.

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.

Re: form_remote_tag not submitting using XMLHttpRequest

yes I have that in the layout file

<html>
<head>
<title>Data Entry Module</title>
<%= stylesheet_link_tag 'application' %>
<%= javascript_include_tag "prototype", "effects" %>

</head>


I see the prototype even in the HTML page that is generated by the Firefox browser
Here is the HTML page
<html>
<head>
<title>Data Entry Module</title>
<link href="/stylesheets/application.css?1188566301" media="screen" rel="Stylesheet" type="text/css" />
<script src="/javascripts/prototype.js?1188571980" type="text/javascript"></script>
<script src="/javascripts/effects.js?1169252196" type="text/javascript"></script>

</head>
<body>
<h1>Data Entry Module</h1>
<div id="vnavsite">
    <ul>

    <li id="am-intro"><a href="/user/logout">Home</a></li>
     <li id="am-application"><a href="/am_applications/list">Applications</a></li>
   </ul>
     
</div>


<h1>Application Modification </h1>

<div id="show_item">

</div>

<form action="/am_applications/update/10360" id="uj_element_1" method="post">

   <table border=0>
<tr>
<td>
<label for="am_application_business_application_name">Business application name</label>
</td>
<td>
<input id="am_application_business_application_name" name="am_application[business_application_name]" size="30" type="text" value="3rd Party Portal" />
</td>
</tr>


</table>
<!--[eoform:am_application]-->

  <input name="commit" type="submit" value="Save Changes" />
</form>

<a href="/am_applications/show/10360">Show</a> |
<a href="/am_applications/list">Back</a>

</body>
</html>


If you see the HTML the form will be submitted with method=post.
Since I  am using form_remote_tag I thought that it should submit using xmlHTTPrequest

Re: form_remote_tag not submitting using XMLHttpRequest

ooooh

i think i may know whats wrong.  The method signature for form_remote_tag changed slightly in 1.2 so those docs wont work completely for your project.  They used to use :html as the key for the url instead of :url.  I remember pulling half of my hair out when i upgraded to 1.2 because of that.

Try:

<%= form_remote_tag :html => { :action => :update, :id => @am_server},
            :update => "show_item",
            :complete => visual_effect(:highlight, "show_item") %>

hopefully that will work big_smile




--jake

Last edited by jake3030 (2007-09-04 19:31:52)

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.

Re: form_remote_tag not submitting using XMLHttpRequest

Jake,

Thanks for the reply. I am surprised that rails api still talks about using the url too.
Yes you are right I am also about to pull my hair. This has been very frustrating.

I did try to change the url to html so now my edit looks like

<div id="show_item">

</div>

<% form_remote_tag :html => { :action => "update", :id => @am_application},
               :update => "show_item",
             :complete => visual_effect(:highlight, "show_item") do %>
   <%= render :partial => 'form' %>
    <%= render :partial => 'relation' %>
  <%= submit_tag 'Save Changes' %>
<% end  %>


Now when the html is generated I see the onsubmit function too.
Here is the HTML sample
 <form action="update" id="#&lt;AmApplication:0x52af0f8&gt;" method="post" onsubmit="new Ajax.Updater('show_item', '', {asynchronous:true, evalScripts:true, onComplete:function(request){new Effect.Highlight(&quot;show_item&quot;,{});}, parameters:Form.serialize(this)}); return false;">

However now when the edit page is rendered and I make changes and press on Save chagnes button. Nothing happens.

As per the logic in my controller update action I should get a response of either success or no_success on the same screen. I checked my database the changes are also not being saved.

Just so that you have a complete picture here is my relevant code for the controller

  def update
    @am_application = AmApplication.find(params[:id])
  set_relations
  if @am_application.update_attributes(params[:am_application])
       if request.xml_http_request?
        render :action => 'new_update', :layout => false
       else
        redirect_to :action => 'list', :app_name => params[:app_name], :maintained_by => params[:maintained_by], :sort_name => params[:sort_name] 
      end
  else
       render :action => 'edit_error', :layout => false
  end
     
  end

Even if I do a  render :text => "hello" as first line on the update action, the text is not rendered on the browser.

I am sure I am creating some silly error.

Re: form_remote_tag not submitting using XMLHttpRequest

At last I found out what was wrong. It was not to do with using :html in the form _remote_tag.

Now my edit.rhtml looks like it used to

<% form_remote_tag :url => {:action => "update", :id => @am_application},
               :update => "show_item",
               :complete => visual_effect(:highlight, "show_item") do %>
   <%= render :partial => 'form' %>
   <%= render :partial => 'relation' %>
   <%= submit_tag 'Save Changes' %>
<% end  %>

The problem was that I had started playing with UJS and the vendor directory in my project had a directory named unobtrusive_javascript. Some how it was getting hold of the form_remote_tag and html that was being generated had
<form action="/am_applications/update/10360" id="uj_element_1" method="post">

and no Ajax.updater. I removed the  unobtrusive_javascript directory from my vendor directory and voila the form_remote_tag starts working.
Now the html generated looks like
<form action="/am_applications/update/10360" method="post" onsubmit="new Ajax.Updater('show_item', '/am_applications/update/10360', {asynchronous:true, evalScripts:true, onComplete:function(request){new Effect.Highlight(&quot;show_item&quot;,{});}, parameters:Form.serialize(this)}); return false;">

I am posting this so that anyone in my shoes do not have to pull their hair.