Common Rails and JavaScript Views

I am a little confused why this is not the problem that is most often addressed, but here.

I have a Rails application that usually generates my views. I have dynamic content on an interface.

I want to make an AJAX call, return JSON and populate the dynamic parts of the application. I would use the JS template library for this.

I DO NOT want to make an AJAX call, Rails processes the view and sends all the HTML back. I want the response to be JSON, so that it reduces processing and posting time.

The problem is that the HTML that I am generating dynamically is the same HTML that I statically statically load on the Rails page (I think AJAX pagination). HTML, naturally, in my ERB files. But when I generate more content (say, page 2), I obviously cannot access the ERB files and must duplicate the HTML in the JavaScript template file.

Is there a solution that allows me to share my views between Rails and JS? (I understand that trying to mix ERBs with JS rendering is a bit uncomfortable ... loops, conventions, etc.)

Do people usually agree to render HTML and send it by wire? Or do they inconveniently execute JavaScript when loading a page for rendering using JS templates?

+8
javascript ruby-on-rails templates
source share
1 answer

In the past, I faced the same problem. I was able to solve this with a Mustache: http://mustache.github.com/ At that time there was no easy way to set a mustache with rails, so I had to code something together. It looks like there is now a lib here: https://github.com/goodmike/mustache_rails3

Update: It looks like there is now a good stone to execute it called stache: https://github.com/agoragames/stache

+3
source share

All Articles