Ruby Forum Ruby on Rails > RJS : find the parent's id

Posted by joserwan (Guest)
on 07.08.2008 20:49
(Received via mailing list)
Hi,

I have a HTML like this :

<div id="user_1">
    <p class = "data_1"></p>
    <p class = "data_2"></p>
</div>
<div id="user_2">
....

In my controller, I have a "update_data" method, called every x
seconds from a periodical_call_remote :

I want to take all the elements having the class "data_1", and replace
their html with :

render :partial => data_1, :local => {:user_id => user_id}, where
user_id is the id of the parent of p.data_1

But I didn't find how to to catch the parents id of p.data_1 !

Does someone have an idea ?


Thank you
Posted by Hubert Łępicki (Guest)
on 08.08.2008 09:58
(Received via mailing list)
page["data_1"]
Posted by joserwan (Guest)
on 08.08.2008 14:22
(Received via mailing list)
Thank you for your response but :

In this case, it will look for the element wich have the id "data_1".

And if I have 50 users, I'll have 50 "data_1" classes.

I would like a way to find the css equivalent of "#user_1 .data_1"

I tried :

page.select("user_1").each{|elt|
            elt.select("p.data_1").each{|u_elt|
              page.replace_html u_elt, "test"
            }
          }

But it doesn't work... :-(
Posted by Frederick Cheung (Guest)
on 08.08.2008 15:21
(Received via mailing list)
On 8 Aug 2008, at 13:21, joserwan wrote:

>
> page.select("user_1").each{|elt|
>            elt.select("p.data_1").each{|u_elt|
>              page.replace_html u_elt, "test"
>            }
>          }
>

I'm coming into this half way, but that selector you've got there is
wrong (that would select tags called user_1)

In pure prototype,

$('user_1').select('p.data_1').each(function(element){ ...})

There are limits to what rjs can map to javascript, something like

page.select('#user_1 p.data_1').each {|elt| elt.update 'test'}

would probably do it, but I'd probably stop fighting rjs and write it
by hand.

Fred
Posted by joserwan (Guest)
on 08.08.2008 15:32
(Received via mailing list)
Found it ! :D

render :update do |page|
     page['user_1'].down('p.data_1').replace_html "test"
end


It's not the transcription of the prototype style you told me, but it
works...

I haven't tried, but
$('user_1').select('p.data_1').each(function(element){ ...})
should be
page['user_1'].select('p.data_1')...

On Aug 8, 9:21am, Frederick Cheung <frederick.che...@gmail.com>