Ruby on Rails, json vs js ajax響應

[英]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)?


1 个解决方案



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.




  © 2014-2022 联系我们: