views:

752

answers:

1

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?

+1  A: 

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?

Matt Briggs
I was hitting issues with things like entire pages being returned instead of partials. Entirely possible it was my fault. What, pray tell, are js.erb files?
midas06
if you want a good, high level overview of how to use jquery with rails, check out this screencast http://railscasts.com/episodes/136-jquery erb isn't tied to html, you can serve up pretty much anything as your view and use erb to template it. js.erb just means javascript file with <%= %> blocks.
Matt Briggs
Actually i feel its better not to watch that screencast as its made too simple. Many issues such as cross-browser accept-header correction and that all your requests should include the auth token are not covered in that cast. I would suggest this instead: http://codetunes.com/2008/12/08/rails-ajax-and-jquery/ Also try out taconite. I am a big fan. Its so much more easier to use compared to js.erb/rjs templates. You can perform multiple DOM manipulations using results from single AJAX call. And not many changes from before. All you have to do is replace js.erb to xml.erb and you are good to go!
Shripad K
i like the code tunes link, but the very thought of taconite makes my skin crawl. to each his own i guess ;-)
Matt Briggs
Totally my fault. I'd been doing things the hard way. js.erb files it is.
midas06
"If you use the jquery driver in rails3, your rjs files will be outputting jquery" - that's wrong, to get your rjs files output jQuery JavaScript you will need something like http://github.com/aaronchi/jrails (haven't figured yet how this plays with Rails 3 though)
dolzenko
@dolzenko: you are right, i don't think I had written a rails 3 app at the time I wrote this, just read about what was there. ill correct the post
Matt Briggs