I heard a bunch of rails developer saying that RJS is evil. I've never used it since I always managed to do what I wanted using classic javascript or jquery so I didn't pay attention. Now I'm getting into some legacy code and there's RJS all over the place.
So... is it true? What are the drawbacks/advantages of using RJS?
Let's talk about what RJS is before getting into whether or not it's evil.
RJS applies the same level of abstraction to high functioning Javascript libraries that ActiveRecord supplies for SQL. However the RJS coverage for Javascript Libraries is nowhere near as complete as ActiveRecord's coverage of SQL adaptors.
Rails ships with RJS support for Prototype/Script.aculo.us only. However, there are plugins either available or in development to support other Javascript libraries. For example JRails rewrites the Prototype based helpers to work with jQuery. Similar plugins exist for mootools and probably Dojo.
The people who consider RJS to be evil, are generally the ones who aren't comfortable with it producing Prototype code, or those who feel they can accomplish things easier with raw Javascript.
RJS isn't perfect, just as ActiveRecord isn't perfect, every so often you have to drop down to writing raw Javascript or SQL to get your job done. Again like ActiveRecord, the more comfortable you get with the advanced options the more you can accomplish without writing the raw code.
One wonderful thing about RJS is that they are essentially views, that produce Javascript. It is very easy to extract out RJS into partials that can be included as necessary, either as responses to controllers or as a part of custom Javascript functions included in the page. This makes the code much more DRY allowing for simpler maintenance.
Personally I use RJS frequently. I find it the perfect way to touch plenty of DOM elements at once. It comes with the double bonus of allowing me to create AJAX rich sites, without writing much Javascript. Then again I hate writing Javascript.
Considering that I replace my underlying library of Prototype with JQuery on my Rails projects, I have found RJS to be quite useful. Now, it can be a pain sometimes as passing JavaScript back to the server to be executed is not exactly mainstream yet.
However, I have found no problems with it RJS in general. The only complaint I have is that I usually have to mix both RJS and plain old Javascript into my .rjs files, so it's a tad bit pointless. But it does give you a clean place/way to handle your Javascript effects and AJAX calls, so I think as a "standard place to put your code", it's quite nice.
I don't know if I'd go so far as to say evil, but RJS (or any server side language generating JS) wouldn't be my first choice. I prefer to write JS by hand. With jQuery I actually enjoy writing JS and keeping my JS in application.js just feels clean.
To expand a little... I see RJS as an unnecessary abstraction. I want to know JavaScript and jQuery. I want to know how to manipulate the DOM and how to make AJAX calls. And with my JS/jQuery knowledge I can move to another framework easily and not wonder if the framework will handle my JS for me.
RJS is nice mostly because it is easy to integrate into Rails projects. To keep things simpler and keep file count low, you can embed it into your controllers and it has a lot of easy to use helpers from the prototype/scriptaculous libraries. It feels a lot more Ruby-like.
It does mean its not as cleanly separated from your normal Rails code, as it gets mixed in with the rest of your code pretty quickly. It also requires a lot more external libraries are getting included via prototype and scriptaculous js files.
Some of the jQuery stuff is very clean. The syntax is pretty crazy, but it means you can pull your js completely out of your pages/controllers (unobtrusive js) which is a much cleaner/compartmentalized way of doing things.
Whats more, jQuery looks like javascript. So you don't get that weird mix of javascript and Ruby code. I like Ruby. I do not like Javascript. But I like the mix of the two even less. If you know JS, it will look familiar to you.
Ryan Bates has a screencast on converting RJS to jQuery. Might give you a good idea of the difference between the two syntactically: http://railscasts.com/episodes/136-jquery
If you are not familiar with JS(or such frameworks like Prototype), but you need an AJAX functionality - RJS is the best way for this. Another advantage to use RJS is a speed. Write RJS code easy and fast.
I used RJS in all Rails projects before. Now i got more familiar with Prototype(and jQuery) and it's the reason why i'm write JS code now. I need this, because controller that has a lot of RJS lost it's productivity. And move out RJS code into JS was the first step to scale controller.
No one can say to you absolutely, what is the best way - use RJS or not. Everyone should choose own way.
For example, i prefer use RJS in admin part(where is no need to scale anything) of my app, and write JS for frontend part.
RJS isn't "evil", but I think the issue with it is twofold:
It's hard (impossible?) to do unobtrusive JavaScript with RJS. If you are writing a Javascript-heavy application and have a lot of logic, and that logic changes, you're going to have to change a fair number of files instead of only one. Also, and this is personal preference, it's rather ugly to see tags with compressed Javascript dotted throughout it.
RJS abstracts away Javascript, which can lead to ignorance of the language. The idea behind RJS was to enable a developer to be able to write everything for a web application (apart from the HTML and CSS which a designer would probably tackle) using just one language: Ruby, but in practice this falls short in the same way that it's possible, but not recommended, to create ASP.NET applications by dragging and dropping controls or using a lot of heavy code generating Wizards. If all you need is a simple solution that requires a sprinking of Ajax then RJS works fine.
RJS is a good tool when you are just getting started with Rails and need some "quick and dirty" Ajax effects that are used sparingly (for example, the canonical comment for a Blog that fades in on the same page). Once you start requiring heavy Javascript usage then RJS becomes more of a liability because it is shielding the developer from something that they really should be trying to understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With