Forum: Ruby on Rails change an xml_http_request to http_request

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
charlysisto (Guest)
on 2005-11-18 13:16
(Received via mailing list)
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'
      render :action => 'edit'

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 ?

jamis (Guest)
on 2005-11-18 16:27
(Received via mailing list)
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

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

- Jamis
carlos.okada (Guest)
on 2005-11-18 19:32
(Received via mailing list)
Hello Jamis

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

Thank you!


2005/11/18, Jamis B. <removed_email_address@domain.invalid>:
jamis (Guest)
on 2005-11-18 19:44
(Received via mailing list)
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:

   <script type="text/javascript">
     function handleComplete(request) {
       var instruction = request.getResponseHeader('X-Instruction')
       if(instruction == "error") {
         $('ajax_div').innerHTML = request.responseText
       } else {

   <div id="ajax_div"></div>
   <%= form_remote_tag :complete=>'handleComplete(request)',
               :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
       headers['X-Instruction'] = "error"
       render :partial => 'error'

- Jamis
Charly (Guest)
on 2005-11-18 20:50
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...
carlos.okada (Guest)
on 2005-11-24 16:25
(Received via mailing list)
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
list in Basecamp). My problem is: Whe validation fails, I want to update
<div> 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
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,
how can I give feedback to the user?



2005/11/18, Jamis B. <removed_email_address@domain.invalid>:
This topic is locked and can not be replied to.