That it is better to ajax in rails with controller return json or return js.erb?
which is faster and why?
That it is better to ajax in rails with controller return json or return
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think speed is the issue here (both options will perform more or less equally).
The keys are maintainability and testability. What do you prefer?
Javascript (worst, rjs, a weird JS/Ruby mixture) scattered all over views on the server side of you app (very, very hard to test).
Centralized JS code that gets data from the server and that can be developed and tested as a unit.
I definitely prefer this second option: on the AJAX section of your app make the server return JSON and manage the responses with Javascript in the client. Now that Rails ships with CoffeeScript by default (hint: use also underscore), it’s also a joy to write client code.