Let's talk about what RJS is before getting into whether or not it's evil.
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.
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. – WayneM Nov 21 '09 at 1:21 1 A great answer, thank you!
– Igor Zinov'yev Nov 23 '09 at 11:52 +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 Nov 27 '09 at 1:49 "The people who consider RJS to be evil, are generally the ones who aren't comfortable with it producing Prototype code" You mean producing Javascript code? – tokland Dec 17 '10 at 20:40 tokland, Yes and no.
While RJS does produce Javascript, the default behaviour was (at the time of writing this post, I'm not sure if this is still true) to produce Javascript that is heavily dependent on the Prototype library. Javascript would fit just as well as jQuery or any other library where Prototype was used in this case. I wanted to make the distinction that RJS produces more than just Javascript.
– EmFi Jan 17 at 13:14.
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.
Thanks for the answer! Can you put jQuery/classic javascript into a . Rjs file?
– marcgg Nov 17 '09 at 17:46 1 @marcgg - If you are using JRails (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 dev-journal.3dmdesign. Com/development/… for more info.
– Topher Fangio Nov 17 '09 at 18:10.
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 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 Nov 24 '09 at 5:25 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 Nov 24 '09 at 7:12 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 Dec 20 '09 at 6:23.
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.
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 Nov 24 '09 at 5:15 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 Nov 24 '09 at 19:49.
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.
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.
– MattMcKnight Nov 24 '09 at 5:12 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 S Nov 26 '09 at 21:34.
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.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.