xmlhttprequest for updating

Hello,

I am updating an object with an AJAX form:

view:

<% remote_form_for(@sample, :update => "content2") do %>
  <p>
    parameter: <br />
    <%= select("sample","parameter",Parameter.find(:all).collect {|p|
[p.description.gsub('_', ' '),p.id]},{},{:size => 4, :multiple =>
true}) %>
  </p>

    <%= submit_tag "add" %>
<% end %>

once form is sent, object is saved in def update in my controller:

controller:

def update
    @sample = Sample.find(params[:id])

      if @sample.update_attributes(params[:sample])
        flash[:notice] = 'Sample was successfully updated.'
        redirect_to(@sample)
      else
        render :action => "edit"

      end

  end

my problem is that redirect_to(@sample) call the show method via a
HTML request, so all my page is refreshed when the view show.html.erb
is displayed...which is not what i want as i sent the form through
AJAX in order to update the DOM "content2"

Is it possible to change redirect_to so that the call of def show is
done through xmlhttprequest and only my partial _show.html.erb is
displayed (I already have a show.js.rjs)?
I tryied to play with format in update but i always get an argument
error when request is done via AJAX.

def update:

respond_to do |format|
      if @sample.update_attributes(params[:sample])
        flash[:notice] = 'Sample was successfully updated.'
        format.html {redirect_to(@sample)}
        format.xml_http_request :get, "show",:id => @sample
      else
        render :action => "edit"

      end

May be i am not using correctly the xml_http_request function.

thank you for your help.

        format.xml_http_request :get, "show",:id => @sample
May be i am not using correctly the xml_http_request function.

You are correct. I don't know what xml_http_request is actually supposed
to do, but this should read something more like:

respond_to do |format|
      format.html { redirect_to(person_list_url) }
      format.js
      format.xml { render :xml => @person.to_xml(:include => @company)
}
    end

format.xml_http_request { ... } would be looking for a template
something like: show.xml_http_request.erb

Of course in reality, it just breaks.

respond_to do |format|
      if @sample.update_attributes(params[:sample])
        flash[:notice] = 'Sample was successfully updated.'
        format.html {redirect_to(@sample)}
        format.xml_http_request :get, "show",:id => @sample
      else
        render :action => "edit"

      end

This also feels wrong to me. Maybe something more like:

if @sample.update_attributes(params[:sample])
  flash[:notice] = 'Sample was successfully updated.'
  respond_to do |format|
    format.html {redirect_to(@sample)}
    format.js
  end
else
  render :action => "edit"
end

if @sample.update_attributes(params[:sample])
  flash[:notice] = 'Sample was successfully updated.'
  respond_to do |format|
    format.html {redirect_to(@sample)}
    format.js
  end
else
  render :action => "edit"
end

Oops, I take this back. It should be more like:

  # PUT /people/1
  # PUT /people/1.xml
  def update
    @person = Person.find(params[:id])

    respond_to do |format|
      if @person.update_attributes(params[:person])
        flash[:notice] = 'Person was successfully updated.'
        format.html { redirect_to(@person) }
        format.js
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @person.errors, :status =>
:unprocessable_entity }
      end
    end
  end

I don't know where my head was on that previous post. Sorry about
that...

Hi Robert,

I indeed did try with format.js. It occurs an error: template is
missing, Missing template samples/update.js.erb.

as I say i already have a show template and show.js.rjs that is doing
everything necessary if def show is called by an AJAX request. that
why i would like to call def show via an xmlhttprequest but from def
update in the controller ( i am not sure i am really clear) and i
think that is what xml_http_request is supposed to do (but i can be
wrong as i am new to AJAX and co).

Hi Robert,

I indeed did try with format.js. It occurs an error: template is
missing, Missing template samples/update.js.erb.

as I say i already have a show template and show.js.rjs that is doing
everything necessary if def show is called by an AJAX request. that
why i would like to call def show via an xmlhttprequest but from def
update in the controller ( i am not sure i am really clear) and i
think that is what xml_http_request is supposed to do (but i can be
wrong as i am new to AJAX and co).

format doesn't actually help you here: the ajax request is genuinely
requesting html. What you can do is

if xhr?
...
end
(xhr? is an alias for xml_http_request?)

which can tell if the request was submitted by prototype.

Fred

Hi Frederick,

format doesn't actually help you here: the ajax request is genuinely
requesting html. What you can do is

if xhr?
...
end

what i should write if xhr? is true?

something like:
render :partial =>show, :id => @sample

if i write that the def show is not call, neither show.js.rjs.
thanks