RJS help for setting input value

I'd like an interface similar to the del.icio.us one for tags, but I'm stuck when trying to add the tag name to an input field. What I'm trying to do is:

page.select('#tag_names').each do |element|    if element.value.to_s.empty?      element.value = @tag.name    else      element.value = element.value.to_s + ", #{@tag.name}"    end end

but it doesn't work ... Suggestions ?

TIA,    ngw

I would test whether element.value gives you access to the DOM element in the way you expect.

Here's a quote from Cody Fauser's RJS Templates for Rails that may have some bearing on what you're trying to do:

"When using a direct element proxy rather than a string passed to <<, the generator assumes that the proxied call is a method call and automatically adds (). This means that you can't directly proxy an element's property, such as element.innerHTML. page.select('#elements span').any('allVisible') do |value, index| page << '!value.visible()' end"

Dave

Very close, thanks ... It seems to me that I can't fetch or use what's inside the value attribute of the input field, it doesn't return a string but a JavascriptElementProxy that doesn't have a to_str/to_s method. I need to check if that value is empty, and if not I need to use it for the new value ... I will never be able to get out from this alone :stuck_out_tongue:

TIA,    ngw

Very close, thanks ... It seems to me that I can't fetch or use what's inside thevalue attribute of the input field, it doesn't return a string but a JavascriptElementProxythat doesn't have a to_str/to_smethod. I need to check if thatvalueis empty, and if not I need to use it for the newvalue... I will never be able to get out from this alone :stuck_out_tongue:

Any luck on getting the value? I am running into the same issue.

Thanks, Greg

Was this issue ever resolved? if so I would love to know how!! Jason.

gblasko wrote:

Was this issue ever resolved? if so I would love to know how!! Jason.

At the time the RJS runs you cannot find the value of an element: the rjs is running on the server, but the info is on the client's page. So for example, if page[:something].empty? ...

or anything like that is impossible. You can however generate javascript that will check a certain condition, for example page << "if($F('foo') == 'bar'){" page[:baz].hide() page << "}"

I waffled about this at some length here:

Fred

Thanks, for the quick reply - I actually read your article earlier on thanks.

Hmm, now lets see if I am getting this right. When I write somthing in the RJS file the page object takes my DLD and transforms it into javascript which is immediatley sent out to the browser which then does the rendering it has been asked for.

I hope I got that bit right?

As it is possible to send snippits of javascript to the browser and ask it to get me a value of a DOM back, then I ask by self why this request cannot be handled by the javascript engine in rails.

The only logical explination to me is that asking the browser and telling the browser are 2 completely different things.

So that would mean that the rails javascript engine has been written to handle all the "tell stuff" - but it can't handle the "ask stuff" i.e. that engine is probably missing.

Could it be that the underlying problem is that the messaging (responder, initiator sequence) is entirely differnt depending on what is being done (asking or telling).

If all this is understandable and the answer is (yes this is how it works) then I will be relieved to know.

Thanks a million Jason

Frederick Cheung wrote:

Thanks, for the quick reply - I actually read your article earlier on thanks.

Hmm, now lets see if I am getting this right. When I write somthing in the RJS file the page object takes my DLD and transforms it into javascript which is immediatley sent out to the browser which then does the rendering it has been asked for.

I hope I got that bit right?

Yup.

As it is possible to send snippits of javascript to the browser and ask it to get me a value of a DOM back, then I ask by self why this request cannot be handled by the javascript engine in rails.

The only logical explination to me is that asking the browser and telling the browser are 2 completely different things.

So that would mean that the rails javascript engine has been written to handle all the "tell stuff" - but it can't handle the "ask stuff" i.e. that engine is probably missing.

Could it be that the underlying problem is that the messaging (responder, initiator sequence) is entirely differnt depending on what is being done (asking or telling).

I think you're overcomplicating things. Rails generates javascript for you. It doesn't evaluate it. It has neither a javascript engine, nor access to all the state of the browser. There isn't any messaging either, just code generation. There's some clever stuff going on with RJS, but that's mostly to make things flow nicely, so that you can write

page[:foo].hide or page[:foo].show

rather than something like

hideElement('foo') or callElementFunction('foo', 'hide')

The trick here is that page[:foo] is not the dom element with id foo. It's a magic proxy object that knows that when you call hide on it it should generate $('foo').hide

You could send the javascript back, have the browser evaluate it and post back a result, but obviously incurs a server round-trip and would be completely inappropriate for this (not to mention the fact that that request would be handled by another mongrel etc...)

Fred

Hi,

Asd Asd wrote:

I have a problem with rjs. After submit the form, rjs not run. I would like clear a text_field_tag. Here is my code:

Your code looks OK at first glance. Check to make sure you don't have another DOM element on the page with an id/name of 'message'.

HTH, Bill

Bill Walton wrote:

Hi,

Asd Asd wrote:

I have a problem with rjs. After submit the form, rjs not run. I would like clear a text_field_tag. Here is my code:

Your code looks OK at first glance. Check to make sure you don't have another DOM element on the page with an id/name of 'message'.

HTH, Bill

I haven't other Dom element in page with an id/name of 'message'. It's incredible.

darkhck