RJS newbie - need help with iterating / existence test


#1

Greetings!

I’m new to Ajax and am trying to get a basic app working as a vehicle
for learning it. The app is an extension of the cookbook tutorial. My
intent is to be able to add a list of ingredients for a recipe. The
page to add ingredients uses Ajax. As ingredients are added to the page
they’re displayed in a “green-bar” format like that used in the Depot
tutorial.

Ingredients are added to the page using form_remote_tag to update a

that contains 3 s: one for the ingredient name, one for quantity, and one that contains a link to delete the ingredient. The delete link uses link_to_remote which in turn uses an RJS view to remove the item from the page.

The stage I’m at now is that, when an item is removed from the page, it
messes up the green-bar effect. So I need to iterate through the

s, changing their CSS class.

I’ve got the change to class working (thanks to help from Ed Frederick)
on a single item with:

page << “$(item3).className = ‘Listline1’;”

(In figuring this out, I’m entering 3 items, deleting the 2nd, then
changing the class of item3 which is now 2nd in the list)

So now I to figure out how to iterate through the remaining items,
either by starting at the top or working from the deletion point down.
In either case, I need to anticipate that this might not be the first /
only deletion and that there will be gaps in the item numbering
sequence. So I need to test for an item’s existence before I pipe the
line above (with the appropriate substitutions for ListlineY) into the
page.

Questions…

To iterate through the list, will I be using Ruby, Javascript, or what?

Is there a simple existence test? I’m hoping for something like if
item#{x}.exists?

Am I on the right track here?

Thanks in advance for any assistance / advice. Advice on a good book to
put me on track would be especially appreciated!

Best regards,
Bill


#2

Hi Bill,

Been meaning to write you back on the previous emails, no time
yet–soon, hopefully.

In short, you can just use Ruby in your RJS template for iteration.

Generic Example:

@items.each do |item|
page << “$(#{item.id}).className = ‘#{(some condition on item) ?
‘class1’ : ‘class2’}’;”
end

Cheers,

Ed

Ed Frederick - edwardfrederick.com


#3

Hi Ed,

Thanks. I’ll give it a shot.

Best regards,
Bill

----- Original Message -----
From: “Edward F.” removed_email_address@domain.invalid
To: removed_email_address@domain.invalid
Sent: 2006-04-10 11:09 AM
Subject: Re: [Rails] RJS newbie - need help with iterating / existence
test

Hi Bill,

Been meaning to write you back on the previous emails, no time
yet–soon, hopefully.

In short, you can just use Ruby in your RJS template for iteration.

Generic Example:

@items.each do |item|
page << “$(#{item.id}).className = ‘#{(some condition on item) ?
‘class1’ : ‘class2’}’;”
end

Cheers,

Ed

Ed Frederick - edwardfrederick.com

On 4/10/06, Bill W. removed_email_address@domain.invalid wrote:

that contains 3 s: one for the ingredient name, one for quantity,
and
one that contains a link to delete the ingredient. The delete link uses
link_to_remote which in turn uses an RJS view to remove the item from the
page.

The stage I’m at now is that, when an item is removed from the page, it
messes up the green-bar effect. So I need to iterate through the

s,
changing their CSS class.

I’ve got the change to class working (thanks to help from Ed Frederick) on
a
single item with:

page << “$(item3).className = ‘Listline1’;”

(In figuring this out, I’m entering 3 items, deleting the 2nd, then
changing
the class of item3 which is now 2nd in the list)

So now I to figure out how to iterate through the remaining items, either
by
starting at the top or working from the deletion point down. In either
case, I need to anticipate that this might not be the first / only
deletion

Am I on the right track here?

Thanks in advance for any assistance / advice. Advice on a good book to
put
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails