I was wondering what are the best methods and practices to do AJAX with rails?
I read this article:
http://chadfowler.com/2011/02/09/how-rails-developers-do-ajax-with-jquery-in-2011
The article points out 4 main possibilities:
- JQuery Templates
- backbone.js
- SammyOnRails
- mustache.js
I am sure this list is not complete. I am searching for a clean way to build AJAX in my Rails 3.1 application.
What is a clean and flexible method to use AJAX with rails?
This question is too broad to answer well in this format. In short: it boils down to personal opinion. Rails will help you build anything from a website with some ajax to a single-page app with a backend-api. Simply put, it won’t get in your way.
Don’t focus so much on all the tools and instead focus on managing the client side of your app just like the server side. Plan for maintainability, write dry code, etc. If you only need a few sprinkles of ajax here and there, you probably won’t benefit from any of the javascript frameworks you mentioned. If you’re client side app is really growing in complexity, then bring in a framework to assist with the complexity.
As too the frameworks you mention:
You may consider mustache or jquery templates if you find yourself building (or updating) many html elements manually. Don’t worry about templating until using jQuery’s html manipulation api gets to messy or unwieldy.
You may consider using backbone, sammy, or spine if you find your communication between the server/client getting too hard to manage or your client side behavior getting very complex.
Without more details, it’s hard to give you much more than that.