# Change an xml\_http\_request to http\_request

**URL:** <https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274>\
**Category:** Rails\
**Created:** [November 18, 2005, 12:16pm UTC](https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274 "2005-11-18T12:16:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlysisto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@charlysisto](https://www.ruby-forum.com/u/charlysisto)\
**Post date:** [November 18, 2005, 12:16pm UTC](https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274/1 "2005-11-18T12:16:50Z")

</div>

Hi,  
I use ajax to edit or create entry in my DB : a div box opens on top of  
the page (a bit like a pop up) with all the fileds to fill. Now to  
display properly an error message in the div box I use the remote tag  
for the ‘update’ or ‘create’ action  
edit.rhtml :  
\<%= form\_remote\_tag :update=\>‘ajax\_div’,  
:url=\>{:controller=\>‘matters\_crud’, :action =\>:update,:id =\> @matter }  
%\>  
\<%= render\_partial ‘form’ %\>  
\<%= submit\_tag ‘Edit’ %\>  
\<%= end\_form\_tag %\>

matter\_crud\_controller.rb :  
def update  
@matter = Matter.find(params[:id])  
if @matter.update\_attributes(params[:matter])  
flash[:notice] = ‘Matter was successfully updated.’  
render :action=\>‘confirm’  
else  
render :action =\> ‘edit’  
end  
end

The thing is, if their isn’t any error I would like the hole page to  
refresh, like in a normal http request so my new or updated entry  
appears properly.  
One solution I thought of is this:  
when update or save is successfull I render ‘confirm’ which has  
‘onLoad=window.location.reload( false )’ but that doesn’t seem to work  
(it does though with onClick for example).

So my question is how could I make an xml\_http\_request behave as a  
normal http request when condition x is met. I’ve seen in the pragmatic  
book this is possible when browser has javascript disabled. Of course  
it’s not the same here since the condition to be evaluated is on the  
server side.  
Any ideas for a workaround ?

Thanks

---

<div class="post-metadata">

**Author:** ![charlysisto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@charlysisto](https://www.ruby-forum.com/u/charlysisto)\
**Post date:** [November 18, 2005, 3:27pm UTC](https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274/2 "2005-11-18T15:27:23Z")

</div>

When I’ve needed something like this, I will typically manage things  
manually. Instead of specifying the :update element, I will handle  
all of that in a :complete handler by hand. The :complete handler  
checks the value of a custom header (‘X-What-To-Do’, for example). If  
the header is ‘error’, then I update the ‘ajax\_div’ element with the  
request.responseText data. If the header is ‘ok’, then I execute  
window.location.reload(false).

Does that make sense? If not, let me know and I can post a more  
detailed example.

- Jamis

---

<div class="post-metadata">

**Author:** ![charlysisto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@charlysisto](https://www.ruby-forum.com/u/charlysisto)\
**Post date:** [November 18, 2005, 6:32pm UTC](https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274/3 "2005-11-18T18:32:01Z")

</div>

Hello Jamis

I have the same plablem Charly have. Could you pls elaborate a little  
more  
on how to implement the :complete handler?

Thank you!

Okada.

2005/11/18, Jamis B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![charlysisto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@charlysisto](https://www.ruby-forum.com/u/charlysisto)\
**Post date:** [November 18, 2005, 6:44pm UTC](https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274/4 "2005-11-18T18:44:06Z")

</div>

On Nov 18, 2005, at 10:31 AM, Carlos Y. Okada wrote:

> Hello Jamis
> 
> I have the same plablem Charly have. Could you pls elaborate a  
> little more on how to implement the :complete handler?
> 
> Thank you!
> 
> Okada.

Sure, here’s a snippet from the hypothetical RHTML file:

 \<%= form\_remote\_tag :complete=\>'handleComplete(request)', :url=\>{:controller=\>'matters\_crud', :action =\>:update, :id =\> @matter} %\> \<%= render\_partial 'form' %\> \<%= submit\_tag 'Edit' %\> 

Then, in your controller, you just do:

def update  
@matter = Matter.find(params[:id])  
if @matter.update\_attributes(params[:matter])  
headers[‘X-Instruction’] = “ok”  
render :nothing =\> true  
else  
headers[‘X-Instruction’] = “error”  
render :partial =\> ‘error’  
end  
end

- Jamis

---

<div class="post-metadata">

**Author:** ![charlysisto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@charlysisto](https://www.ruby-forum.com/u/charlysisto)\
**Post date:** [November 24, 2005, 3:25pm UTC](https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274/5 "2005-11-24T15:25:54Z")

</div>

I’m using form\_remote\_tag from a page that lists a few records to allow  
users to create/update one of the records in place (very much like the  
Todo  
list in Basecamp). My problem is: Whe validation fails, I want to update  
the

 showing the error\_message\_for and the form again, but when update succeed, I want to refresh the whole page.

I’m trying to use the solution Jamis sugests bellow, and it works. The  
only  
problem is that if an error occurs in controller during create/update  
actions, nothing happens at the browser. I can see the error in the log,  
but  
how can I give feedback to the user?

Tks

Okada.

2005/11/18, Jamis B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![charlysisto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ad7895/32.png) [@charlysisto](https://www.ruby-forum.com/u/charlysisto)\
**Post date:** [November 18, 2005, 7:50pm UTC](https://www.ruby-forum.com/t/change-an-xml-http-request-to-http-request/48274/6 "2005-11-18T19:50:53Z")

</div>

Thanks a lot for your answer Jamis, I had sent a reply earlier asking  
for more insight but I dont understand how gmane handles responses.  
Maybe it will appear soon…
