I'd be interested in hearing how others handle this too.

I usually get everything working initially with regular non-xhr rails show/edit/update, then add a mini backbone view on topto hook into button events and 'ajaxify' it after the fact:

$(document).ready(function() { new SomeWidgetView().render(); });

... which handles click events, form submits, ajax calls etc. I test the backbone views with jasmine, and cucumber can run through the workflow with or without @javascript for integration testing. I'm guilty of the same "render :text => '' if request.xhr?" in the controller though in the case of POST and PUT, and wonder if there's a better way to handle it?

I also feel a bit dirty doing AJAX GET requests which return HTML, and dumping it into the DOM, but going the JSON route and writing an extra jscript template to reproduce the same HTML that I've already got in a rails view doesn't seem very DRY either.

Any thoughts on AJAX with graceful degradation while remaining DRY?

dave

Michael Pearson wrote:
Hi,

I'm looking for recommendations as to the best way to create & manage buttons that perform an action on an object and then update a portion of the page with a response from the server.

This was relatively simple in Rails 2.3 land: the now removed link_to_remote method (http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/link_to_remote) would automagically generate javascript that would ask your Rails app for a snippet and then replace part of the page with that snippet.

It was, of course, messy as hell, which I think is why it got removed.

Looking for something that does something similar in 3.x land hasn't gotten me very far: the consensus seems to be "write your own damn javascript". We've done so so far, but it's never been quite as easy as the old helper methods.

Also, I'm haunted by the "doing it wrong" spectre: the way we're doing it is simply aping the way the 2.3 helpers used to work, except with hand written UJS rather than generated RJS.

The example I'm working on right now is a button that, while editing a user, allows the administrator to forgive a user's past invoices. The button is within an existing form. The code, right now, is bloody terrible:

= link_to "Cancel Outstanding Invoices", cancel_outstanding_invoices_user_path(@user), :class => "btn btn-danger", :id => "cancel-outstanding-invoices", :remote => true, :method => :post
          :javascript
$('#outstanding-invoices').bind('ajax:success', function(event, data) { $('#outstanding-invoices').html(data); });

The Rails action simply performs a "render :text =>".

There's a whole bunch of better ways I can think of doing the above - even ways that allow me to make the Javascript code completely generic. However, I'd rather see how others do it first.

--
Michael Pearson


--
You received this message because you are subscribed to the Google Groups "Ruby or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rails-oceania?hl=en.

--
You received this message because you are subscribed to the Google Groups "Ruby or 
Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to