Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails, json vs js ajax response

I'm curious what is the best way to respond to an ajax request, is it sending json, and then parse it at the client side (for example with pure) or should I render javascript at the server side and return the result(with a js.erb template)?

like image 838
jonepatr Avatar asked Dec 09 '11 01:12

jonepatr


1 Answers

Using js.erb templates tends to be simpler and encourages you to keep your application logic in your Rails controllers. For traditional applications, where little to no application logic is handled in javascript, this can be a good thing.

On the other hand, using JSON encourages you to develop your Rails app more as an API server for a javascript-heavy client application. If you're looking to do your core CRUD actions over ajax and rarely reload the page, this is probably fits better with your application's style.

Which is best in a particular situation depends on both the type of application you're looking to build and the attributes of the particular request.

like image 112
Luke Avatar answered Oct 20 '22 01:10

Luke