Forum: Ruby on Rails degrading ajax calls - how?

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.
Shilo A. (Guest)
on 2009-02-22 20:16
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)

<tr id="prod_<%=h product.id %>
    onclick="<%= remote_function(:url => { :action => 'start_edit',
                                 :id => @product} )%>" >
  <td><%=h product.name %></td>
</tr>

# 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?
Robert W. (Guest)
on 2009-02-23 18:28
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.
This topic is locked and can not be replied to.