# Xmlhttprequest for updating

**URL:** <https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219>\
**Category:** Rails\
**Created:** [September 10, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219 "2008-09-10T16:10:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![da991319](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@da991319](https://www.ruby-forum.com/u/da991319)\
**Post date:** [September 10, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219/1 "2008-09-10T16:10:30Z")

</div>

Hello,

I am updating an object with an AJAX form:

view:

\<% remote\_form\_for(@sample, :update =\> “content2”) do %\>

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

```
<%= 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.

---

<div class="post-metadata">

**Author:** ![da991319](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@da991319](https://www.ruby-forum.com/u/da991319)\
**Post date:** [September 10, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219/2 "2008-09-10T16:30:45Z")

</div>

> ```
> 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

---

<div class="post-metadata">

**Author:** ![da991319](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@da991319](https://www.ruby-forum.com/u/da991319)\
**Post date:** [September 10, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219/3 "2008-09-10T16:40:50Z")

</div>

> 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…

---

<div class="post-metadata">

**Author:** ![da991319](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@da991319](https://www.ruby-forum.com/u/da991319)\
**Post date:** [September 10, 2008, 4:56pm UTC](https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219/4 "2008-09-10T16:56:24Z")

</div>

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).

On Sep 10, 8:40 am, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![da991319](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@da991319](https://www.ruby-forum.com/u/da991319)\
**Post date:** [September 10, 2008, 5:11pm UTC](https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219/5 "2008-09-10T17:11:38Z")

</div>

On 10 Sep 2008, at 15:55, da991319 wrote:

> 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

---

<div class="post-metadata">

**Author:** ![da991319](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ebca7d/32.png) [@da991319](https://www.ruby-forum.com/u/da991319)\
**Post date:** [September 10, 2008, 7:04pm UTC](https://www.ruby-forum.com/t/xmlhttprequest-for-updating/148219/6 "2008-09-10T19:04:10Z")

</div>

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

On Sep 10, 8:59 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
