HTML return in AJAX Rails

After reading David Heinemeier Hansson 's blog post about a javascript server-generated blog post , I decided to consider how I approach AJAX calls in my Rails applications. David suggests creating a .js.erb template, which is just javascript embedded with ruby ​​code generated on the server, and not perform any DOM manipulation on client javascript.

Another approach is, of course, to just do everything on the client side and (for example) return a JSON object representing the updated object from the server, and use javascript for all DOM manipulations.

I do not like the first approach for two reasons:

1) I use HAML and Coffeescript in my application and feel that using vanilla javascript and ERB irreversibly inflate my codebase with code of a different language (it is possible to create .coffee.haml templates, not js.erb, I don’t know)

2) I really don't like the idea of ​​“clogging” my view folder with what is essentially javascript files, with a small amount of built-in rubies.

The second approach, which David talks about in his blog, is very dependent on client-side javascript, which can lead to bloated JavaScript code on the client side and, possibly, the need to create client-side templates, which in the worst case scenario can mean almost doubling the number of templates.

The approach I decided to go for (and want to ask if this is a completely stupid way to do this) is as follows:

1) Set the remote: true flag so that links and forms use AJAX to send to the server.

2) In my controller, treat everything as html and simply render without a layout if the request is an AJAX request: render partial: '<partial-name>', layout: false if request.xhr? . It simply returns partial HTML with a ruby ​​code score.

3) In the asset javascript file (e.g. <partial-name>.js.coffee ) listen to ajax:success and add the HTML from the response.

I like this approach because (in my pretty simple application) it allows me to save all my code in HAML / Coffeescript and avoid any javascript templates.

I understand that this problem can be of a different nature if the complexity of the application increases, but I still feel that this is the right question: this is a bad way to implement the AJAX-based architecture for the Rails application (and if so, why? That is the reason why returning HTML instead of JSON from an AJAX call is a bad idea?) or is this something I should continue to use?

Thanks: -)

+6
source share
2 answers

Your approach seems pretty accurate to me. However, I would change 1 pt. Instead of using remote: true, I would use a direct jQuery ajax call.

 $(function(){ $('#some_link').click(function() { $.ajax({ // some parameters here }) .done(function(data){ $('div').html(data); }); }); }); 
+2
source

if you use remote: true, it sends a js request instead of an html request.

then, when the controller method is executed, it looks for the js.erb or js.haml file, which has the same name as the controller action that just finished executing.

in this file you can write the code "js" to perform any actions that need to be performed after the completion of the controller action, for example, changing a partial or updated view, etc.

if you have a function in the javascript asset file, you can also call this function.

+1
source

All Articles