Forum: Ruby on Rails change an xml_http_request to http_request

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1de696caf10af0090291d7804f07c33b?d=identicon&s=25 charlysisto (Guest)
on 2005-11-18 12:16
(Received via mailing list)
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
992fe8c19bbbc27f2b562a9f96efc03d?d=identicon&s=25 jamis (Guest)
on 2005-11-18 15: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
window.location.reload(false).

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

- Jamis
47c4771d9d8782067b57faf3aed18a44?d=identicon&s=25 carlos.okada (Guest)
on 2005-11-18 18:32
(Received via mailing list)
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 Buck <jamis@37signals.com>:
992fe8c19bbbc27f2b562a9f96efc03d?d=identicon&s=25 jamis (Guest)
on 2005-11-18 18: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 {
         window.location.reload(false)
       }
     }
   </script>

   <div id="ajax_div"></div>
   <%= form_remote_tag :complete=>'handleComplete(request)',
        :url=>{:controller=>'matters_crud',
               :action =>:update,
               :id => @matter} %>
     <%= render_partial 'form' %>
     <%= submit_tag 'Edit' %>
   </form>

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
1de696caf10af0090291d7804f07c33b?d=identicon&s=25 Charly (Guest)
on 2005-11-18 19: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...
47c4771d9d8782067b57faf3aed18a44?d=identicon&s=25 carlos.okada (Guest)
on 2005-11-24 15: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
Todo
list in Basecamp). My problem is: Whe validation fails, I want to update
the
<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
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 Buck <jamis@37signals.com>:
This topic is locked and can not be replied to.