Rails future & Javascript - ruby-on-rails

David H.H. announced recently that jquery is going to be the default in Rails 3.1, and that Prototype helpers / RJS are going into a gem.
What does that mean for the future? Should we progressively forget about things like javascript helpers, RJS, and all these fun parts of rails? And start coding with jquery/json in mind?
I'm not against that at all, but I have to admit I find RJS really fun to use...

Yes, unobtrusive JavaScript has won. Many people write now directly JS / jQuery code in ERB templates. jQuery code to replace some DOM node with a partial is only slightly longer than equivalent RJS code. Regarding JS helpers, it should be quite easy to replace all of them with unobtrusive JS code. Check how data-remote is handled in jQuery Rails driver.
This trend makes sense because web apps have more and more JS code which cannot be written in RJS. It is better to use two languages than three.
If you don't like JavaScript syntax then check out CoffeeScript. Which will be definitely more popular than RJS.
Long term future probably belongs to pure JS frameworks like SproutCore. Server side will process only JSON data. However these new techniques will be used only in new apps. Generally it doesn't make sense to upgrade existing apps to the new model.

Related

Can AngularJS architecture interfere with a framework like Ruby on Rails at some point?

I have a web application that has most of it's logic on ruby on rails, and lately I've been getting good at AngularJS but I'm not a master at it. What I like about angular is that it lets you be very declarative on your javascript code knowing if an element is being referenced on the html unlike jquery. Also it has nice filtering options but that would not be needed cause rails handles that on the server-side. Is AngularJS meant to work as a stand alone framework or it can coexist with server-side frameworks?.
This is more of a discussion question, so not an ideal SO question.
Angular and Rails can happily coexist.
You can use the asset pipeline and load Angular, in the same way that you would any other JS library.
There is a good tutorial here, also, this Railscast is quite handy although it is a bit out of date now.
My advice is to have a crack at it, and then come back to SO and post specific bugs or challenges you are hitting.

Pure HTML template solution for Rails?

I love HAML, however recently our projects have come under some scrutiny with regards to reliance on non-html structured templates. I thought I'd take it a step further by asking the question, "How can we use pure HTML design based templates in Ruby on Rails?"
The closest thing I've found so far is a very interesting project that has it's most recent update from 2010 called Kwartz from the author of Erubis.
Is there a project that upholds this pure HTML isolation for designers that is up-to-date and viable on Rails 3.2.x?
Your designers are correct that HAML does not have widespread HTML tool support.
A really excellent solution IMHO is Handlebars. It is simpler than HAML, and will work with more HTML tools because Handlebars emphasizes moving code out of the page template and into the controller. This also is good for writing maintainable pages with designers and also for security.
Handlebars is led by Yehuda Katz, who helped write Rails 3, is a core contributor to JavaScript, and is currently working on Ember.js which also leverages Handlebars.
http://handlebarsjs.com/
You are either going to do something with the templates, right? As in, the designer gives you the template, you strip out the parts that already belong to app/views/layouts/application, inject the necessary ruby to get your data into the view etc.
So what's the problem? Let the designers provide their templates in HTML, you convert them to HAML when using in the app, instead of converting them to ERB.

Rails Prototype and Unobtrusive Javascript

I've been programming rails for only a few months and was first introduced to RJS in 2.3.8 rails. Though things have changed slightly and am a little confused with this "unobtrusive javascript". From reading google, I'm understanding it as like removing the javascript inline to a seperate file.
From what I understood of .rjs was it was already doing this.
Can someone explain the difference, if there is one? I'm still trying to make that leap over to unobtrusive JS cause it seems like it's the rails 3 way but I'm having trouble making that jump. Or is unobtrusive javascript "pure" js and not like the ruby way as I've understood that .rjs is a wrapper for javascript. Should I take the clue that I need to start learning java script? I know very little of javascript.
Thanks in advance for any advice/tips.
Unobtrusive means not doing things inline, as you said. RJS is something different, basically ruby helpers that generate javascript. What unobtrusive means for rails is that instead of generating js inline, it will instead decorate the dom objects with information enough to figure out what to do, and then use event delegation as an alternative technique to just dumping a script tag into the middle of the page.
This results in much cleaner output, and potentially better performance, both client side (don't have to re-run javascript all the time if you are using event delegation), and ajax-wise (not doing things inline means you don't need to push javascript over the wire as well as your html).
RJS is a whole different thing, and was pretty much un changed by that move. Typically, I find that people who aren't that familiar with how javascript works prefer the rjs approach, since it just does the job. If you are really serious about your js, chances are you will either be using js.erb files, or just doing returning json from the server and dealing with everything else fully client side.

Is unobtrusive RJS files in Rails 3 considered a good idea?

I'm working on implementing javascript functionality in my rails 3 app. Now that rjs is supposedly unobtrusive (I honestly don't know a lot about rjs), is it still "evil"?
It seems to me the downside may be a lack of testability, but partial page updates via rjs seem to be easier than jumping through the rails hurdles to make ajax requests via jquery.
Thoughts? Examples?
rjs isn't evil per se (unless you consider prototype evil, which is a valid point of view), its more one of those things that shouldn't be used without understanding what its doing under the hood. You shouldn't use rjs as a way to avoid learning javascript, and once you learn javascript you tend to naturally lean towards js.erb files anyways. page.refresh is actually pretty much the only thing I use rjs files for anymore.
Out of curiosity, what hoops are you jumping through to do Ajax requests?

Latest and accepted ajax techniques in Rails

What is the present state of Rails Ajax?
What frameworks and technologies should one use when working with Rails now?
Rails seems to evolve so rapidly that one might not be able to keep up.
Is it prototype and RJS or something else?
The point of RJS is that you don't really need to keep up with a framework, that the javascript functions are abstracted into Ruby for you to use.
That said, prototype is still the default choice, but there are plugins (http://ennerchi.com/projects/jrails) to implement RJS functions in jQuery. Of course, you could forgo the abstractions entirely and write the javascript however you'd like.
Personally, I find myself trying to stick to the Rails' default methods, and if I need a method that goes outside what RJS provides, making sure I am implementing it properly (ie. not duplicating what someone else has done), and usually doing it in prototype as to not have to load multiple javascript frameworks.
As said before, Rails' default javascript library is (and will probably always be) Prototype/Scriptaculous. However, when Rails 3.0 is released sometime around the beginning of May, it should be more accepting of other libraries such as jQuery.
If you don't like RJS, you can use a .js.erb extension and write javascript that will be sent through the erb template engine. This is my preferred way of doing things these days. You can see an example of this, and jQuery in this episode of Railscasts.

Resources