Topic: Need a exemple of RJS

Hi,

I'm discovering Rails 3 and I pain to implement RJS.

  1. Wwhat is the extension of RJS file (.js.rjs or .js.erb...) ?

  2. Should I add some code in controller to activate RJS functionalities ?

  3. What is the syntax of RJS templates ?

  4. Where can I find documentation or tutorial concerning RJS for Rails 3 ?

I'm trying to test RJS functionalities with this code :

show.js.rjs wrote:
update_page do |page|
  page.insert_html :bottom, 'notice', 'Cool'
end

But nothing happens, I don't understand.

Thanks

Last edited by GRoguelon (2010-11-19 06:12:43)

Re: Need a exemple of RJS

IMHO, I prefer writing javascript (usually jquery) directly. Even though writing RJS is tempting I find
plain javascript to be much more powerful (i.e. you are currently forced to use prototype if you use RJS).

Anyway:

  1. It should be .response_format.language so the extension for "returning" javascript written with RJS is .js.rjs. Using .js.erb you can respond with javascript written with erb (which is what I use to respond with jquery)

  2. You should at least specify that the action you are calling responds with js (when called via ajax).

  3. The syntax you specified in the code example is correct

  4. I think RJS hasn't changed much in Rails 3... You can read something about RJS vs Javascript here and here. The official Rails 3 api documentation is obviously the main resource (JavaScriptGenerator, PrototypeHelper)

I hope this was the info you were looking for...

Re: Need a exemple of RJS

mty wrote:

Anyway:

  1. It should be .response_format.language so the extension for "returning" javascript written with RJS is .js.rjs. Using .js.erb you can respond with javascript written with erb (which is what I use to respond with jquery)

  2. You should at least specify that the action you are calling responds with js (when called via ajax).

  3. The syntax you specified in the code example is correct

  4. I think RJS hasn't changed much in Rails 3... You can read something about RJS vs Javascript here and here. The official Rails 3 api documentation is obviously the main resource (JavaScriptGenerator, PrototypeHelper)

Thanks for the answers. I've already found this links but it seems that RJS isn't detailled on the web...

mty wrote:

IMHO, I prefer writing javascript (usually jquery) directly. Even though writing RJS is tempting I find
plain javascript to be much more powerful (i.e. you are currently forced to use prototype if you use RJS).

RJS is attractive when we hate to write javascript like me... But it's time to learn jQuery.

What is the easiest framework to discovery (jQuery or Prototype) ? I saw that Prototype is included by default in RoR 3.

Thanks again for your answer.

Re: Need a exemple of RJS

GRoguelon wrote:

Thanks for the answers. I've already found this links but it seems that RJS isn't detailled on the web...

I agree... It seems like RJS is considered a marginal rails feature.

GRoguelon wrote:

RJS is attractive when we hate to write javascript like me... But it's time to learn jQuery.

What is the easiest framework to discovery (jQuery or Prototype) ? I saw that Prototype is included by default in RoR 3.

I had the same problem... RJS was really attractive to me too because I've never been very good at coding
in javascript. I also found prototype counter-intuitive and difficult to learn. Everything changed when
I began studying jQuery. It's fun, simple, intuitive and extremely powerful (from ui to more "dev" stuff like ajax and json).
Prototype is included in RoR because it started from Rails itself. Even so, I personally find jQuery much more easy to learn.

Don't forget there are also other js powerful frameworks (like dojo and mootools), so maybe you should look around to find what fits your coding style. If you choose jQuery, there is an easy way of including it in a rails project: jquery-ujs

Obviously when you leave Prototype, you need to leave RJS as well.

Re: Need a exemple of RJS

I was a modules developer for PHPboost and I had to use script.aculo.us and I hadn't liked that! So, I know a few the different frameworks but jQuery is very attractive and with the variety of plugins available...

Anyway, javascript is almost inescapable in web development of elegant application.

Re: Need a exemple of RJS

I absolutely agree...
On this subject, I love the way data-* attributes play nicely with rails3, html5 and jQuery's .data() method.
Unobtrusive javascript is the way to go smile

Re: Need a exemple of RJS

Can you detail a bit more about "data-" ?

Re: Need a exemple of RJS

Start by reading this, in particular the section about html5. Then look for rails3 data-remote...