Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well this is a very good question because rewriting code in 2 different languages is not very DRY to say the least.</p> <p>This problem happens very often especially for validations that SHOULD be done on the client to provide instant feedback, and MUST be done on the server for security purposes.</p> <p>On the client side you do not have the choice for the language and have to use JavaScript. This implies that if you want to reuse your code you will have to have at least a JavaScript version.</p> <p>One option is to use HotRuby <a href="http://hotruby.yukoba.jp/" rel="nofollow noreferrer">http://hotruby.yukoba.jp/</a> to run a (large) subset of Ruby directly in the client. Interestingly enough this is very efficient: <a href="http://ejohn.org/blog/ruby-vm-in-javascript/" rel="nofollow noreferrer">http://ejohn.org/blog/ruby-vm-in-javascript/</a> and should therefore suit your needs. Similarly RubyJS <a href="http://www.ntecs.de/projects.html" rel="nofollow noreferrer">http://www.ntecs.de/projects.html</a> converts Ruby code to JavaScript. Another one is rb2js <a href="http://rb2js.rubyforge.org/" rel="nofollow noreferrer">http://rb2js.rubyforge.org/</a>.</p> <p>Another option is to run a JavaScript interpretor on your server, using the SpiderMonkey engine <a href="http://www.mozilla.org/js/spidermonkey/" rel="nofollow noreferrer">http://www.mozilla.org/js/spidermonkey/</a> or the v8 engine: <a href="http://code.google.com/p/v8/" rel="nofollow noreferrer">http://code.google.com/p/v8/</a> both of which are very efficient.</p> <p>Finally you could convert JavaScript to Ruby using RKelly: <a href="http://tenderlovemaking.com/2007/04/15/converting-javascript-to-ruby-with-rkelly/" rel="nofollow noreferrer">http://tenderlovemaking.com/2007/04/15/converting-javascript-to-ruby-with-rkelly/</a>.</p> <p>I believe the first option is easier to implement and will please most Rails programmers as one only writes Ruby code. The second option might be more efficient on the client but at the cost of other inefficiencies on the server. The third option might be the most efficient and will appeal more to JavaScript programmers or to reuse existing JavaScript code.</p> <p>Although this goes beyond the original question (which is about Rails), I would also like to point out the JavaScript MVC <a href="http://javascriptmvc.com/" rel="nofollow noreferrer">http://javascriptmvc.com/</a> which prevents this problem altogether. Sometimes the solution to a problem is to remove the problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload