Newbie AJAX rjs syntax question

I’m brand new to Ruby and to Rails, and I’m running through Pragmatic’s
“Agile Development” V.2 book and am having problems with the rjs files
in their tutorial.

First problem:

page[:cart].replace_html :partial => ‘cart’, :object => @cart

This line stopped the AJAX request. The action completed without
trouble, but when it hit this line in the rjs file, the ‘cart’ div did
not refresh. I checked the API, and changed the syntax thusly:

page.replace_html ‘cart’, :partial => ‘cart’, :object => @cart

And now it works.

A couple more similar problems, fixed by changing syntax. This is the
current one:

page.select(‘div#notice’).each { |div| div.hide }

Shouldn’t this work? This book is beta, but the version I have is 2
days old. Is the syntax already depreciated or am I doing something
wrong here? The Webrick server shows no errors, and no errors in the
development.log. Also nothing on the book’s “errata” page, which leads
me to believe the problem is mine.

THanks!

Matt R. wrote:

page.select(‘div#notice’).each { |div| div.hide }

I don’t really know if there’s any syntax error, but div#notice suggests
that you’ve got div with id=“notice”, which you can only have one on a
page (id is unique), so you don’t have to use select, you can just do:
page[‘notice’].hide.
If you want to hide children of div#notice do page.select(‘div#notice
div’).each… or use ‘div.notice’ if notice is a css class.

I’m rjs newbie myself, so i may be wrong.

Hey Szymek -

Thanks for the response. Here’s the issue: the page loads dynamically,
so sometimes there’s a div with a “notice” id, and sometimes there’s
not. So page[‘notice’].hide might crash the request if there was not a
div to hide, in theory, and the way I had it wouldn’t, right? In
practice, on my server, the way I had it doesn’t work, whether or not
the div exists. The thing that really puzzles me, though, is that I’m
not coming up with these lines of code. I’m taking them directly from a
tutorial that was just published 3 days ago. And I keep running into
these errors. Might I have my server configured wrong?

szymek wrote:

Matt R. wrote:

page.select(‘div#notice’).each { |div| div.hide }

I don’t really know if there’s any syntax error, but div#notice suggests
that you’ve got div with id=“notice”, which you can only have one on a
page (id is unique), so you don’t have to use select, you can just do:
page[‘notice’].hide.
If you want to hide children of div#notice do page.select(‘div#notice
div’).each… or use ‘div.notice’ if notice is a css class.

I’m rjs newbie myself, so i may be wrong.