UJS in Rails 3 Beta

Hi All,

Does anyone know if there are any good examples of how to use the unobtrusive javascript functionality in Rails 3. I've read the release notes but I can't seem to get things working.

I've just set up a simple Rails app with a Post model. In the "_form" partial I have add the ":remote => true" parameter, as required. When I submit the form it saves the record asynchronously, as expected but it doesn't redirect to the appropriated page afterwards.

Any ideas what I'm doing wrong?

Thanks! Phil

Useful link I found:-

http://joshhuckabee.com/jquery-rails-3

Does the HTML5 stuff in Rails3 degrade to something else for browsers that don't support it yet? I've read all the Rails3 news I can find but don't see this answered anywhere.

I don’t think that Rails 3 is using anything that wouldn’t already be supported by older browsers. It’s not like it’s sticking in by default, just using some special named parameters. A large goal with HTML5 was backward compatibility.

Maybe there will be some good examples available soon. Hope so.

I just did a post on using UJS in Rails 3 - http://therailworld.com/posts/26-Using-Prototype-and-JQuery-with-Rails3-UJS-

<snip> so if you wanted to use JQuery or MooTools you would need to do make your own solution. </snip>

Wrong. There is JRails which uses jQuery and provides all same javascript helpers.

http://github.com/aaronchi/jrails

In addition, any jQuery you want to run through the page object is as simple as:

page << "$('#foo').bar();"

JRails is just a drop in replacement for PrototypeHelper in Rails releases prior to 3.

Rails 3 looks is phasing out PrototypeHelper in favour of UJS. The goal of UJS is to eliminate all inline Javascript. The end result of this is that all your favourite javascript generating helpers will be deprecated. link_to_remote, form_remote_for, link_to_function, and the rest will be gone.

Yes. a driver file will need to be created for each of mootools, jquery or any other javascript frame work to use the new :remote => true syntax of Rails 3. But they're pretty simple files that don't change too much from the prototype example that ships with rails ("rails.js").

You can use the new prototype_legacy_helper or a slightly modified JRails with Rails 3 to restore the old way of doing Javascript in Rails. But it wouldn't be the Rails 3 way of doing things.

By the time Rails 3 gets a proper release there will be drivers for all the popular Javascript frameworks.

I'm going to retract a part of my response, particularly the bit stating that plugins/gems like JRails are unnecessary in Rails 3. After doing a little more digging on the issue, it looks like JRails will still be required if you're looking to use RJS templates to generate JQuery.

This goes for other JS frameworks like Mootools or Dojo as well

The underlying meaning of this statement is; Rails as a framework use to favour a certain Javascript framework (Prototype). This doesn't mean that you alone would need to write your own solution for JQuery, but someone, somewhere *did* have to write JRails. Likewise, if you wanted to use Mootools, someone would need to write a solution for it. Rails 3 and UJS solve this problem, among others.

Greg Donald wrote:

Wrong. There is JRails which uses jQuery and provides all same javascript helpers.

GitHub - aaronchi/jrails: jRails is a drop-in jQuery replacement for Prototype/script.aculo.us on Rails. Using jRails, you can get all of the same default Rails helpers for javascript functionality using the lighter jQuery library.

JRails is obtrusive javascript, where rails 3 implements unobtrusive javascript. Using JRails along with Rails 3 just seems...wrong. I always thought using JRails in itself was kind of wrong, just because JQuery focuses more on unobtrusive javascript. This was the reason I started using JQuery in the first place.

I think the correct solution is to look at the new Implementation of the unobtrusive prototype helpers in Rails 3, and write similar unobtrusive JQuery, using JRails to help.