Change an xml_http_request to http_request

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

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

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. [email protected]:

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

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. [email protected]:

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…