Page.insert_html using element select instead of id?

Hello there,

I’m trying to do something like in a rjs file:

page.insert_html :bottom, page.literal(" $(‘comments_list’).down(0) "),
:partial => “comments/comment_list”, :object => @comment

The problem is that I always get this error: “RJS error: TypeError: null
has no properties”

Sometimes I can’t understand rjs.

Any ideas?

David S.

Sometimes I can’t understand rjs.

Any ideas?

Don’t think that’s going to work… check out the source for the
insert_html method. It has the following line:

record “Element.insert(”#{id}", { #{position.to_s.downcase}:
#{content} });"

So your page.literal isn’t going to do any good as the method is
wrapping it in double quotes…

Doesn’t seem like it would be hard to modify to do what you want
though…

-philip

sometimes this error happens because the html element that you are
trying to access using it’s id does not load yet. You can check if
when you try to access $(‘comments_list’).down(0) the element exists
in the HTML. You can check using the firebug.

On 31 jul, 17:27, David S. [email protected]

Thanks for the quick answer, but nothing yet.

Philip H. wrote:

record “Element.insert("#{id}", { #{position.to_s.downcase}:
#{content} });”

This is the helper source code, but if you go to the Prototype API you
will see:

insert(element, { position: content }) → HTMLElement

So, I understand that you can pass a element. I just can’t get the
helper to evaluate the JS code that lead me to the element.


fRAnKEnSTEin wrote:

sometimes this error happens because the html element that you are
trying to access using it’s id does not load yet. You can check if
when you try to access $(‘comments_list’).down(0) the element exists
in the HTML. You can check using the firebug.

The element ‘comments_list’ exists. It looks like:

What I’m trying to do is, add li elements inside of the ul element
without giving it a Id.

Thanks anyway… still looking for the answer.
By the way, someone have done something similar, but did not work for
me. Look at:

On Aug 1, 1:16 am, David S. [email protected]
wrote:

So, I understand that you can pass a element. I just can’t get the

helper to evaluate the JS code that lead me to the element.

if rjs is getting in the way (sounds like it is) there’s nothing wrong
with just ditching the helpers

fred

Hi,

I gave an ID to the UL element and solved my problem.
But I’m not happy with that… still looking for a more dynamic way to
do it.

Some people could do it using:

page.insert_html :bottom, page.literal("$$(‘p.welcome b’).first()"), “Some item”
and it is not working for me sadly.

thanks,

David S.

chosen wrote:

Easy to solve your unhappyness because other people use the “$$”
function while you just use the “$” function from prototype. $$
(’#comments_list ul’) is waht you need to add, obviously.

Hi chosen, thanks for your attention but did not work as well.

I tried:

page.insert_html :bottom, page.literal("$$(’#comments_list ul’)"), :partial => “comments/comment_list”, :object => @comment

but with no success.

Thanks anyway.

David S.

On Aug 11, 3:19 pm, David S. [email protected]
wrote:

chosen wrote:

Easy to solve your unhappyness because other people use the “$$”
function while you just use the “$” function from prototype. $$
(‘#comments_list ul’) is waht you need to add, obviously.

Have you tried using page.select ? it yields those elements matching
the selector you pass it.

Fred

On 1 Aug., 15:36, David S. [email protected]
wrote:

thanks,

David S.

Posted viahttp://www.ruby-forum.com/.

Easy to solve your unhappyness because other people use the “$$”
function while you just use the “$” function from prototype. $$
(‘#comments_list ul’) is waht you need to add, obviously.

Frederick C. wrote:

Have you tried using page.select ? it yields those elements matching
the selector you pass it.

Fred

Hi Fred,

With this:

page.select(’#comments_list ul’).each do |el|
el.hide
end

I can hide the element.

BUT, if I put “page.insert_html :bottom, el, :partial =>
“comments/comment_list”, :object => @comment” inside the structure it
does not work. It generate a JS code like:

Element.insert("#ActionView::Helpers::JavaScriptVariableProxy:0x7f97a1c5eb18"…

So the JS can’t find the element.

I thank you for your help. But I think I will just put an id to the ul
element and solve the problem. RJS and prototype is difficult and poorly
documented, I can’t understand the true advantage behind rjs and pro.

Best regards,

David S.

On Aug 11, 6:20 pm, Frederick C. [email protected]
wrote:

ditching RJS if you need more than it can give.

oh, and page.literal(“$$(‘#comments_list ul’)”) may not have worked
because $$(‘#comments_list ul’) returns an array whereas insert is
probably expecting a single element. $$(‘#comments_list ul’)[0] might
work better.

Fred

On Aug 11, 5:23 pm, David S. [email protected]
wrote:

Element.insert(“#<ActionView::Helpers::JavaScriptVariableProxy:0x7f97a1c5eb 18>”…

So the JS can’t find the element.

I thank you for your help. But I think I will just put an id to the ul
element and solve the problem. RJS and prototype is difficult and poorly
documented, I can’t understand the true advantage behind rjs and pro.

Prototype is fine, but RJS can only do so much. Nothing wrong with
ditching RJS if you need more than it can give.

Fred

Fred, I think that page.literal is not working for me. Every thing that
I do generates a JS code like:

try { Element.insert("$$( ‘#comments_list ul’).first ", …

But, if the $$( ‘#comments_list ul’).first code is inside “”, the code
will never be evaluated and the insert function will always look for a
element with and id like: $$( ‘#comments_list ul’).first

This might be the big problem… I don’t know.

David S.