Latest and accepted ajax techniques in Rails - ruby-on-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.

Related

Rails future & Javascript

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.

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?

Designing template for Ruby on Rails view. What and where to learn?

I have a project going on, and I am in charge of the front-end design, whereas my developers will work on the back-end with Ruby on Rails.
I do not know Ruby on Rails, and am designing front-end using XHTML, CSS, jQuery, 960.gs CSS Framework. My developer is supposed to take my design and connect the elements of back-end to it, with Ajax too.
What are the things that I should know while designing the template/view so that I won't kick my developers' asses with my design? How to help the connecting of elements painless? I understand I must avoid . Some Ruby on Rails developers also prefer Blueprint CSS Framework over 960.gs.
Any guidance? Thanks.
Generally the Rails templating system is quite flexible and will enable developers to create even complex designs. The CSS framework should not make such a difference. However if they are using Rails 2.x it is markedly easier to use Prototype instead of jQuery. However Rails 3.x is also agnostic to javascript library.
A relatively good overview is the official guide. You might also try out this tool for cutting up your views and layouts.
I can speak from the developer point of view.
The things that piss me off are too complicated css structures, keep it simple and abstract as you can.
The other things is naming classes and ids, in general try to find out what models the developer is using and name your classes and ids accordingly. E.g. for a blog with posts:
#posts .post for a post in the index view and #post for a post in the show view.
I never care what css framework the designer uses, as long as it works.
And lastely, if you design different html pages, be aware that we often have only one or two layouts, that's eg. /views/layouts/application.html.erb and we usually try to keep that number low as possible.
jQuery is find, ever been my preferred choice over rails' own prototype.

Does a "vertical" framework for RoR make sense?

I have been spending some time creating what I called a framework.
It was aimed at the creation of quiz likes games. It was supposed to have two players syncronized and present them a question to solve, etc.
I would like it to be fully customizable so I tried to develop components that can be put in or out of the pages. In the end the elements became slim ruby methods to add a whole bunch of Javascript and CSS to the pages.
Still the Javascript needs to connect to Ruby so methods supporting it are created but they will only be present when the component is present. Some components depend on one another making everything overly complex.
And after this attempt I wonder, is there is not a better and easier way to make a framework aimed to one kind of application on RoR? Or is the concept flawed or RoR in some way flawed?
Ruby on Rails is a framework on its own accord and is "opinionated software". This means that the writers of Rails looked at what would make most sense for creating a web application to them. Many people support the original developers views and so use Rails for their projects as well.
I think your concept of creating a quiz is a good one, but first you need to understand the rails stack. Depending on what you need exactly, you can create either an engine, plugin or whatever.
What I have seen a lot is that you specify what you need in your controller. (How you do that is up to you). All that information is stored in a class variable and transferred to the view where you can render everything you need with some helpers. The hard part is making it all generic enough to be reusable.
But, maybe Rails isn't the right tool for you. Maybe you need something more lightweight like Merb or even Sinatra.
There is no 'flaw' in Rails. Rails is not the 10**1000-in-one tool Java is. It's a framework that tries to do one way very good in a particular way. I think Rails can be the right tool for you, but you need to be skilled enough to wield the tool :)

Resources