Forum: Ruby on Rails RJS newbie - need help with iterating / existence test

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-10 17:41
(Received via mailing list)
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
<div> that contains 3 <span>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
<div>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
41e1579600683eed6c00af9a425268e6?d=identicon&s=25 Edward Frederick (Guest)
on 2006-04-10 18:12
(Received via mailing list)
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
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2006-04-10 19:07
(Received via mailing list)
Hi Ed,

Thanks.  I'll give it a shot.

Best regards,
Bill

----- Original Message -----
From: "Edward Frederick" <epfrederick@gmail.com>
To: <rails@lists.rubyonrails.org>
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 Walton <bill.walton@charter.net> wrote:
> that contains 3 <span>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 <div>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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
This topic is locked and can not be replied to.