# Degrading ajax calls - how?

**URL:** <https://www.ruby-forum.com/t/degrading-ajax-calls-how/161040>\
**Category:** Rails\
**Created:** [February 22, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/degrading-ajax-calls-how/161040 "2009-02-22T19:16:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [February 22, 2009, 7:16pm UTC](https://www.ruby-forum.com/t/degrading-ajax-calls-how/161040/1 "2009-02-22T19:16:14Z")

</div>

Hey - I have a few ajax calls in my app that I’m not really sure how to  
degrade to html. For the most part, these are functions that insert or  
remove elements to/from the dom.

For example, I have my own edit-in-place on the data, which is called by  
start\_edit action. This action renders an rjs template that replaces the  
view with an edit form.

# view (partial)
 { :action =\> 'start\_edit', :id =\> @product} )%\>" \> \<%=h product.name %\>
# controller

def start\_edit  
prod = params[:id]  
params[:row] = “prod\_#{prod.id}”  
respond\_to do |format|  
format.js { render :template =\> ‘rjs/start\_edit’ }  
end  
end

# rjs

page.replace\_html params[:row], :partial =\> ‘edit’, :object =\>  
params[:id]

Any ideas?

---

<div class="post-metadata">

**Author:** ![gte351s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5f9b8f/32.png) [@gte351s](https://www.ruby-forum.com/u/gte351s)\
**Post date:** [February 23, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/degrading-ajax-calls-how/161040/2 "2009-02-23T17:28:20Z")

</div>

Shilo A. wrote:

> Hey - I have a few ajax calls in my app that I’m not really sure how to  
> degrade to html. For the most part, these are functions that insert or  
> remove elements to/from the dom.

> def start\_edit  
> prod = params[:id]  
> params[:row] = “prod\_#{prod.id}”  
> respond\_to do |format|  
> format.js { render :template =\> ‘rjs/start\_edit’ }  
> end  
> end

> Any ideas?

Your start\_edit action is going to need to respond to format.html.  
You’ll then need the response to regenerate the full HTML page that  
represents the desired page state, since you can’t simply manipulate the  
DOM.

I suppose you could store the desired page state in an instance variable  
from the controller action so your view can determine which version of  
the page to draw.

Constants:  
VIEWING = 0  
EDITING = 1

def start\_edit  
@page\_state = EDITING  
…  
…  
end

I’m not really an expert on degrading AJAX to HTML, but this should be a  
gist of it though.
