8

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 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for JSON responses with controllers as an API to the views. Doing it this way decouples the view logic (manipulating the DOM) from controller code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.