RJS and page.select collection size

I need to implement a conditional in my RJS template which looks
something like:

if (page.select(‘row1’).first != null)
page << “new TableRow.MoveAfter(‘row1’, ‘newrow’);”
page << “new TableRow.MoveAfter(‘row2’, ‘newrow’);”

Now, dumb question… My ‘if’ doesnt work, how should I properly check if
the returned collection found row1 or not? (Note: row1 is unique)

What about using length? Such as:

if page.select(‘row1’).length == 0
// etc…


Nope, that converts it to:

And complains that no such method exists.

If I do raw javascript, it seems to work, ex:
page << “var d = $(”#{element_row_id(@chk_edit)}"); alert(d);"

With that I catch the row when it’s present and ‘null’ when it’s not
there. But I’m still trying to figure out how to either incorporate that
or fix the page.select clause.

Alright, I got it to work with raw JavaScript… using one long ugly line
of code:

page << “if (document.getElementById(’#{row1’)) { new
TableRow.MoveAfter(‘row1’, ‘newrow’); } else { new
TableRow.MoveAfter(‘row2’, ‘newrow’); }”

But I’m still curious, is there a nicer way to do this with page.select?



Sounds really good. Do you have a timeline or a subversion repository
browsing on this plugin?



Nope there isn’t at the moment. I’ve been thinking about adding some
kind of support for conditions in my unobtrusive javascript plugin
(which isn’t released yet). I was thinking something like:

page.if(‘thing > 4’) do

because using a real ruby if statement is not possible. What do you


I second that, it would be nice to have the ability to add control logic
based on javascript eval’s.

Waiting for your plugin now! :slight_smile:


It very close (I’ve been saying that for about 2 months now) but there
are a few show stoppers to sort out with it. Also, I’m trying to
refine it by putting it into production use at the moment. The main
purpose of the plugin is to enable unobtrusive style scripting (where
no JavaScript is put into the body of the html) but I’ve ended
implementing almost a total rewrite of the JavaScript Generator and
proxy classes to get it done. Here’s a preview of some of the new

page.onready do

execute some RJS as soon as the DOM is ready (executes before


page.select(“input.thing”, :as => :field) do |field|
field.onkeypress do |event|
page.remote :update => ‘item’
end # get inputs as fields (so you can call Field methods and assign
an event handler with RJS

page.function :func_name, ‘arg1’, ‘arg2’ do |arg1, arg2|
arg1.thing = arg2

create functions which can be assigned to event handlers etc.


page.collection([1,2,3,4,5,6]).each do |item|
end # iterate over collections

and yes, finally some kind of condition statement but im still
puzzling over the API for that.

It’s definately not a replacement for javascript but should give you a
little more power and also gives you the flexibility to use other
library code.

Let me know what you think…and you can expect to see the plugin over
the next 2 or 3 weeks. I’m definately going to have something to show
at RailsConf.



This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs