How to access element attributes using RJS?

For example, in the following block:

page.select(‘.foo’).each do |element|

end

How would I access, just for example, the id attribute of each element
in
the collection?

I’ve randomly tried element.id and element.attributes[‘id’], but neither
of
those has worked. I’ve also been unsuccessful in finding any
documentation
on this, but it seems like such a basic task.

Thanks!
DW

View this message in context:
http://www.nabble.com/How-to-access-element-attributes-using-RJS--tf1951711.html#a5352195
Sent from the RubyOnRails Users forum at Nabble.com.

dw wrote:

those has worked. I’ve also been unsuccessful in finding any documentation
on this, but it seems like such a basic task.

Thanks!
DW

I’m not sure you can at the moment. I’ve been struggling to do a
conditional in my RJS template, and finally found a post saying that you
couldn’t do that because you couldn’t get at the value of the select…

In the examples I’ve seen the each enumerator block has two variables

page.select('.foo).each do |value, index|

page << 'alert(value.innerHTML);'

end

Also, value is used instead of element. Not sure how significant that is
but
give it a whirl.

Regards,
John

----- Original Message -----
From: “Chris T” [email protected]
To: [email protected]
Sent: Wednesday, July 19, 2006 2:52 PM
Subject: Re: [Rails] How to access element attributes using RJS?

A little off topic but… in a view file for lisiting links the logic
goes something like:

<% if @links.size == 0 %>

No links...

<% else %> ## for link in @link .... <% end %>

I have a form on the same page used tro create a new link. In create.rjs
I’m looking for a way to get rid of page[‘no-links’] only if it exists.
I’ve tried

page << if($(‘no-links’)) { page[‘no-links’].remove }

which is obviously wrong. Can someone tell me what to put in create.rjs
to remove #no-links if it exists in the DOM? Many thanks…

Greg

Try this, it assumes your controller increments the links before the rjs
file is processed

page[:no-links].remove if @links.size == 1

----- Original Message -----
From: “Greg” [email protected]
To: [email protected]
Sent: Wednesday, July 19, 2006 3:17 PM
Subject: [Rails] Re: How to access element attributes using RJS?

John W. wrote:

Try this, it assumes your controller increments the links before the rjs
file is processed

Thanks. I’ve attempted the following but it didn’t work:

links_controller.rb:

def create
@link = Link.new(params[:link])
@saved = @link.save
@num_links = Link.find(:all).size
if request.xhr?
page[:listing-status].replace_html “You’ve created #{@num_links}
links in #{@num_cats} categories”
page[:no-links].remove if @num_links == 1 && page[:no-links]
return
end
if @saved
flash[:message] = ‘Link was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

create.rjs

if @saved
page.insert_html :after, “category-#{@link.category_id}”, :partial =>
‘link’, :object => @link
page.visual_effect :highlight, “link-#{@link.id}”
page.replace_html ‘new_link_error’, ‘’
page[‘link_url’].value = ‘http://’
page[‘link_name’].value = ‘’
page[‘link_description’].value = ‘’
else # not saved, present error messages
page.replace_html ‘new_link_error’, error_messages_for(‘link’)
page.visual_effect :appear, ‘new_link_error’, :duration => 0.5
page.visual_effect :highlight, ‘new_link_error’, :duration => 0.5
end

#Error in links_controller#create
NameError: undefined local variable or method `page’

Guess you can’t use page[] in the controller…

Greg wrote:

Guess you can’t use page[] in the controller…

Certainly not true. This works:

#from links_controller.rb
if request.xhr?
render :update do |page|
if @saved
page[‘listing-status’].replace_html “You’ve created
#{num_links} links in #{num_cats} categories”
page[‘no-links’].remove if @num_links == 1 && page[‘no-links’]
page.insert_html :after, “category-#{@link.category_id}”,
:partial => ‘link’, :object => @link
page.visual_effect :highlight, “link-#{@link.id}”
page.replace_html ‘new_link_error’, ‘’
page[‘link_url’].value = ‘http://’
page[‘link_name’].value = ‘’
page[‘link_description’].value = ‘’
else # not saved, present error messages
page.replace_html ‘new_link_error’, error_messages_for(‘link’)
page.visual_effect :appear, ‘new_link_error’, :duration => 0.5
page.visual_effect :highlight, ‘new_link_error’, :duration =>
0.5
end
end #end update
return
end

…I smell a helper! Cheers.

Greg