views:

69

answers:

2

In Ruby on Rails, I have an update method in a controller, that is being called by both ordinary HTTP requests and AJAX requests from many different pages.

For example, a user might just use that controller's edit page, which submits to update by either ordinary HTTP or AJAX, depending on if the user has JS. On the other hand, the user might be calling update from the index method via AJAX.

How do I send separate responses for all these scenarios? A different RJS file for edit and index? Is it a good idea to make separate update methods for each "scenario"?

A: 

If they do the same thing then they don't need any separation at all. If you want to format your responses differently then read up on the respond_to method.

Azeem.Butt
Yes, but for example, the same RJS won't do for both index and edit methods.
It sounds like you have a design problem which you're not really explaining very thoroughly.
Azeem.Butt
A: 

How much do the responses from the different pages have in common? Obviously the JS response call isn't going to return what the html response will. As you probably know, the JS version should be used to update only the portions that need to be updated, while the html version should update the entire page.

If the majority of all html responses are similar / all js responses are similar, you should be breaking the bits that don't appear in all responses into partial. You can toggle their display by pass an extra parameter in the link.

I prefer partial views with the condition around whether to render them or not, but there's no reason you can't do partial RJS. I haven't tested it, but seeing as an RJS file is just straight ruby code you should be able to require another RJS file to include it like a partial.

The code you need to know to make it work:

Adding an extra parameter to a link:

The Old Fashioned way:

<%= link_to_remote "I am a remote link", :url => {:controller => controller, :action => action, :id => @whatever, :last_action => params[:action] } %>

The Restful Way:

<%= link_to_remote "I am a remote link", controller_url(@whatever, :last_action => params[:action])%>

RJS code

page.replace_html :only_replaced_from_edit, :partial => 'was_edit' if params[:last_action] == "edit"
page.replace_html :only_replaced_from_index, :partial => 'was_index' if params[:last_action] == "index"
page.replace_html :not_replaced_from_edit, :partial => 'wasnt_edit' unless params[:last_action] == "edit"

Same goes for regular html requests. Abstract into common/unique partials and load them based on parameters. Should be simpler because you don't need to worry about elements not existing.

EmFi