4

Should AJAX calls that are not RESTful be:

  1. put in the controller that most suits their functionality/view, or
  2. bundled together into their own separate 'Ajax' controller?

I've been doing 1, but I've just read this (2725 diggs) article
http://zygote.egg-co.com/10-dirty-little-web-development-tricks/ (see point 9)
and this chap opts for method 2. But he is a PHP developer though.

One benefit could be that 2 might clean up the routes by doing something like 'ajax/:action' instead of adding members to restful routes.

It seems like a 6.5 of one, half a baker's dozen of the other type thing.

Which option do you go for?

1
  • Thanks for your responses. You echo my own views in that they should go in their own controller. Mr big digg PHP man should change his ways! ;)
    – Jo P
    Jul 29, 2009 at 13:04

2 Answers 2

9

I prefer the first approach:

  1. it's semantically consistent. if an Ajax action involves resource XXX, you (and other coders) will know where to find things in your application, thanks to Rails conventions.
  2. if your application is heavy on Ajax (and nowadays most of them are), you will end up with a behemoth AjaxController that negates the whole RESTful thing. The rest of your controllers will be there just to provide gracefully degraded non-javascript CRUD actions.
  3. Similarly, testing your Ajax controller will tend to be somewhat messy because you will have to set up scenarios --load fixtures, mocks, etc. for each and every "ajaxified" resource of your app.
0

Your "RESTful" controllers probably include new and edit actions, neither of which are actually RESTful, they just provide user interfaces for the create and update REST actions. new and edit don't get a separate NonRestUIController or something, they are kept in their associated resource's controller, keeping your controllers semantically consistent. Similarly, Ajax actions that relate to a certain set of functionality or a certain resource should stay in the associated controller.

2
  • REST has nothing to do with new/edit or create/update. I'm not sure what your point is here.
    – aehlke
    Aug 11, 2009 at 17:31
  • 1
    That's exactly my point. They don't actually have anything to do with REST, but they are kept in so-called RESTful controllers alongside the resources they are associated with to keep things simple. I'm saying it isn't much different to do the same with Ajax actions. Aug 17, 2009 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.