Rails Prototype and Unobtrusive Javascript - ruby-on-rails

I've been programming rails for only a few months and was first introduced to RJS in 2.3.8 rails. Though things have changed slightly and am a little confused with this "unobtrusive javascript". From reading google, I'm understanding it as like removing the javascript inline to a seperate file.
From what I understood of .rjs was it was already doing this.
Can someone explain the difference, if there is one? I'm still trying to make that leap over to unobtrusive JS cause it seems like it's the rails 3 way but I'm having trouble making that jump. Or is unobtrusive javascript "pure" js and not like the ruby way as I've understood that .rjs is a wrapper for javascript. Should I take the clue that I need to start learning java script? I know very little of javascript.
Thanks in advance for any advice/tips.

Unobtrusive means not doing things inline, as you said. RJS is something different, basically ruby helpers that generate javascript. What unobtrusive means for rails is that instead of generating js inline, it will instead decorate the dom objects with information enough to figure out what to do, and then use event delegation as an alternative technique to just dumping a script tag into the middle of the page.
This results in much cleaner output, and potentially better performance, both client side (don't have to re-run javascript all the time if you are using event delegation), and ajax-wise (not doing things inline means you don't need to push javascript over the wire as well as your html).
RJS is a whole different thing, and was pretty much un changed by that move. Typically, I find that people who aren't that familiar with how javascript works prefer the rjs approach, since it just does the job. If you are really serious about your js, chances are you will either be using js.erb files, or just doing returning json from the server and dealing with everything else fully client side.

Related

What do I need to do to convert the views in my Rails app from HAML to ERB?

I have a Rails app that I'm customizing. The more I work on it, the more I both love and hate HAML. I love that HAML's so clean to look at it, I hate that I can't intuitively trouble shoot with Chrome Inspector or Safari Developer Tools. So, I have gathered that conversion to ERB is the best thing for others in my shoes (designers or other mere mortals with basic knowledge of HTML and CSS, wanting to customize the content in this open source Rails app).
What are the steps involved in converting the views from HAML to ERB
and what else will I need to change? ...routes? controllers? anything?
There are a lot of the down-in-the-weeds questions about this, but I'm looking for an overview so I can wrap my head around what to expect.
If you're just converting HAML->HTML/ERB You shouldn't have any need to be changing any controllers, routes or models at all. But I wouldn't necessarily recommend taking that route. HAML is great because it stops people being syntactically incorrect in their HTML. It essentially forces you to not make mistakes.
I don't really understand the issue with troubleshooting through chrome/firefox as all it's outputting is HTML at the end of the day.

Is it possible to hide the contents of a js.erb file in Rails?

Just realized that when I go to foo/:id/method.js my code is being exposed. Is there a way to hide this or redirect "snoopers"?
I've seen it done before. When I figure out what site it was, I'll post it here
See my answer below
If you wanted to mitigate "snoopers", you could minify your Javascript which would obfuscate your code by making it unreadable, but there is no way to completely hide it. It's best just to write safe code, and not be so uptight about people looking at your Javascript, because if they want to see it, they will. Not sure what site you're talking about, but I guarantee you I can access the Javascript.
Here's my little work around for now
Since I'm using a js.erb with Rails. You basically throw in a couple of if statements. I'm working on it now & will test it out

Rails future & Javascript

David H.H. announced recently that jquery is going to be the default in Rails 3.1, and that Prototype helpers / RJS are going into a gem.
What does that mean for the future? Should we progressively forget about things like javascript helpers, RJS, and all these fun parts of rails? And start coding with jquery/json in mind?
I'm not against that at all, but I have to admit I find RJS really fun to use...
Yes, unobtrusive JavaScript has won. Many people write now directly JS / jQuery code in ERB templates. jQuery code to replace some DOM node with a partial is only slightly longer than equivalent RJS code. Regarding JS helpers, it should be quite easy to replace all of them with unobtrusive JS code. Check how data-remote is handled in jQuery Rails driver.
This trend makes sense because web apps have more and more JS code which cannot be written in RJS. It is better to use two languages than three.
If you don't like JavaScript syntax then check out CoffeeScript. Which will be definitely more popular than RJS.
Long term future probably belongs to pure JS frameworks like SproutCore. Server side will process only JSON data. However these new techniques will be used only in new apps. Generally it doesn't make sense to upgrade existing apps to the new model.

Is unobtrusive RJS files in Rails 3 considered a good idea?

I'm working on implementing javascript functionality in my rails 3 app. Now that rjs is supposedly unobtrusive (I honestly don't know a lot about rjs), is it still "evil"?
It seems to me the downside may be a lack of testability, but partial page updates via rjs seem to be easier than jumping through the rails hurdles to make ajax requests via jquery.
Thoughts? Examples?
rjs isn't evil per se (unless you consider prototype evil, which is a valid point of view), its more one of those things that shouldn't be used without understanding what its doing under the hood. You shouldn't use rjs as a way to avoid learning javascript, and once you learn javascript you tend to naturally lean towards js.erb files anyways. page.refresh is actually pretty much the only thing I use rjs files for anymore.
Out of curiosity, what hoops are you jumping through to do Ajax requests?

Latest and accepted ajax techniques in Rails

What is the present state of Rails Ajax?
What frameworks and technologies should one use when working with Rails now?
Rails seems to evolve so rapidly that one might not be able to keep up.
Is it prototype and RJS or something else?
The point of RJS is that you don't really need to keep up with a framework, that the javascript functions are abstracted into Ruby for you to use.
That said, prototype is still the default choice, but there are plugins (http://ennerchi.com/projects/jrails) to implement RJS functions in jQuery. Of course, you could forgo the abstractions entirely and write the javascript however you'd like.
Personally, I find myself trying to stick to the Rails' default methods, and if I need a method that goes outside what RJS provides, making sure I am implementing it properly (ie. not duplicating what someone else has done), and usually doing it in prototype as to not have to load multiple javascript frameworks.
As said before, Rails' default javascript library is (and will probably always be) Prototype/Scriptaculous. However, when Rails 3.0 is released sometime around the beginning of May, it should be more accepting of other libraries such as jQuery.
If you don't like RJS, you can use a .js.erb extension and write javascript that will be sent through the erb template engine. This is my preferred way of doing things these days. You can see an example of this, and jQuery in this episode of Railscasts.

Resources