Topic: Problem with page.insert_html in rjs template

Hi everybody!

I'm trying to highlight invalid entries in an ajax-based form. What i am
trying to do in the called rjs template is this:

  for @invalid_entry in @invalid_entries
    page.insert_html :after ,@invalid_entry,'</span>'
    page.insert_html :before ,@invalid_entry,'<span
class=fieldWithErrors>'
  end

@invalid_entry is the id of an invalid entry (input field). I am trying
to surround it with a span which is formatted by css.
What is generated is this:

<span class="fieldWithErrors"/>
<input type="text" value="21" size="30"
name="worktime_entry[231][hours]" id="worktime_entry_231_hours"
class="worktime_input"/>

The span before the entry is closed (although i don't close the span tag
in the insert_html). The span closing tag is not displayed...

Does anybody see the error in this? Help would be greatly appreciated!

Thanks for reading

Re: Problem with page.insert_html in rjs template

Maybe try?

page.insert_html :bottom ,@invalid_entry,'</span>'
page.insert_html :top ,@invalid_entry,'<span class=fieldWithErrors>'

Re: Problem with page.insert_html in rjs template

Well, the input tag doesn't have a closing tag, which is probably throwing it off. You may need to place a space before the />

If that's not it, it might be looking for an </input> tag. You may want to add it and just see if that solves it. I know it's not ideal, but at least you will know what the problem is.

Railscasts - Free Ruby on Rails Screencasts

Re: Problem with page.insert_html in rjs template

Thanks for your replies! Unfortunately i couldn't solve the problem.

No matter what kind of html tag i insert, it is always closed automatically so it is impossible to surround my input field that way.
Shouldn't the insert_html just insert whatever i tell it? It seems like there is some kind of intelligence in there. Or do you think the automatic closing of the tag happens elsewhere?

Seems like im stuck with this approach. I might have to try something different. Any suggestions for a smart system to mark invalid entries in a ajax-driven form are would be appreciated.

Re: Problem with page.insert_html in rjs template

How is it that you know the resulting html?  If the changes are made with javascript the 'view source' won't cut it.  Are you using the View Source Chart plugin for FF?  I know that one tends to take liberties with the html.

Re: Problem with page.insert_html in rjs template

i use the Firebug plugin for Firefox

Re: Problem with page.insert_html in rjs template

You could try appending the class directly to the element:

page << "$('#{@invalid_entry}').className += ' fieldWithErrors'"

Railscasts - Free Ruby on Rails Screencasts

Re: Problem with page.insert_html in rjs template

above solution works perfectly.

thanks so much ryan!