views:

968

answers:

7

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?

+5  A: 

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.

Topher Fangio
Thanks for the answer! Can you put jQuery/classic javascript into a .rjs file?
marcgg
@marcgg - If you are using JRails (http://github.com/aaronchi/jrails) then you can put JQuery into it. However, classic Javascript will work no matter what library you use. There is a special way to insert it however. Check http://dev-journal.3dmdesign.com/development/qa-how-to-javascript-in-rjs-templates for more info.
Topher Fangio
+3  A: 

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.

Andy Gaskell
RJS is a JavaScript file with embedded Ruby. It's a separate issue from the JavaScriptHelpers etc. which let you do things like:page.insert_html :bottom, 'list', content_tag("li", "Fox")
MattMcKnight
Right, you're writing Ruby code to generate JavaScript and if that works for you, then that's awesome. It's just not for me.
Andy Gaskell
I guess that's not what I call RJS. RJS templates are more like html.erb files- templates for returning JS to the browser to be eval-ed as the response to an AJAX call. They could be all hand coded JS, but putting ruby in there let's you interpolate values from the rest of your program.
MattMcKnight
+1  A: 

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

Lukas
"Whats more, jQuery looks like javascript. So you don't get that weird mix of javascript and Ruby code."This argument is backwards. With the helpers, you don't get the JavaScript code in your code you get:page.insert_html :bottom, 'list', content_tag("li", "Fox")
MattMcKnight
That's if you're arguing for RJS. If I'm arguing for JS, I don't want Ruby code in my JS. There are still a lot of instances where to get proper JS functionality, I end up using JS rather than all these "helpers." Its just easier. And with jQuery, I get to move all of it out of my Ruby code and into Javascript and use the helpers only when necessary.
Lukas
+24  A: 

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.

EmFi
+1 for an excellent and concise response explaining pros and cons. As a RoR newbie I'm always wondering if I should use RJS in my application or use a library like jQuery and handle it myself.
Wayne M
A great answer, thank you!
Igor Zinov'yev
+1: Kudos EmFi! I know nothing about RJS or ruby. (Well OK I know they exist...) I've given a +1 as you discussed the facts about what RJS is and the motivations for why people like and dislike RJS. Well done!
Jason D
+1  A: 

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.

thaold
+1  A: 

RJS isn't "evil", but I think the issue with it is twofold:

  1. 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.

  2. 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.

Wayne M
Isn't the most unobtrusive form of JavaScript the JavaScript you don't even see?
MattMcKnight
RE: Point 2. I know a lot of developers who can't write a web service without using some sort of tool, or do refactoring without the help of an IDE. I think the issue lies with the developer, not the tool
Jeremy Seghi
A: 

RJS is simply the JavaScript equivalent of RHTML (now known as html.erb). It is a template that executes embedded Ruby and returns JavaScript to the browser to update the page. This allows you to have greater controller over what happens as the result of a server side action in an AJAX application. In effect, the results of an RJS call are evaluated by the JavaScript interpreter in the browser. Contrast this with a non-RJS AJAX application where the server returns HTML that is inserted into the page by the callback for the asynchronous request.

The "evil" part is that many people are uncomfortable with "eval", but I think it may also be the result of some kind of confusion.

Many of the responses here seem focused on the JavaScript, Prototype, and Scriptaculous Helpers, which are often used, but are not required, as part of the RJS template. I do make extensive use of those helpers as they flow better with the Ruby code in the templates, but they are not a necessary part of RJS.

MattMcKnight