How to initially hide a drop-down control and have an image control make it visible

Hi,

I've got the following code in my attempt to provide the subject functionality, lines 20-29 in view:

    <%= image_tag "DownArrow.jpg" options = { onclick=page["vendor_droplist"].show } %>     <br>     <div name="vendor_droplist">   <%= select_tag "test",         options_for_select(@current_vendors.collect { |v| v.nickname }),         {:multiple => true} %>     </div>

Until I put in the options on image tag and added the div, this code display a list of vendor nicknames. Now I have several problems:

1. Syntax error in line 20 where an identifier was encountered where a right-paren was expected (see below) 2. I don't know how to make the vendor_droplist initially hidden

I've Googled for "Rails Ajax toggled item" and got a number of hits but no apparent answer. I'll keep poking around the Web, but I appreciate some suggestion(s).

Thanks in advance, Richard

Showing app/views/expenses/new.html.erb where line #20 raised:

compile error K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS/app/views/expenses/ new.html.erb:20: syntax error, unexpected tIDENTIFIER, expecting ')' @output_buffer.concat " "; @output_buffer.concat(( image_tag "DownArrow.jpg" options = { onclick=page["vendor_droplist"].show } ).to_s); @output_buffer.concat "\n"

^ K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS/app/views/expenses/ new.html.erb:20: odd number list for Hash @output_buffer.concat " "; @output_buffer.concat(( image_tag "DownArrow.jpg" options = { onclick=page["vendor_droplist"].show } ).to_s); @output_buffer.concat "\n"

^

Extracted source (around line #20):

17: <p> 18: <%= f.label :vendor %><br /> 19: <%= f.text_field :vendor %> 20: <%= image_tag "DownArrow.jpg" options = { onclick=page["vendor_droplist"].show } %> 21: <br> 22: <div name="vendor_droplist"> 23: <%= select_tag "test",

Trace of template inclusion: app/views/expenses/new.html.erb

RAILS_ROOT: K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS Application Trace | Framework Trace | Full Trace

K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS/app/views/expenses/ new.html.erb:59:in `compile!' K:/_Projects/Ruby/_Rails_Apps/_EIMS/RTS/app/controllers/ expenses_controller.rb:30:in `new'

I think I corrected the div by: 1. giving it an id rather than a name 2. adding display:none to start it off hidden as follows:

    <%# = image_tag "DownArrow.jpg" options = { onclick=page["vendor_droplist"].show } %>     <br>     <div id="vendor_droplist", style="display:none">   <%= select_tag "test",         options_for_select(@current_vendors.collect { |v| v.nickname }),         {:multiple => true} %>     </div>

I think I improved two things in the version of lines 20-26 below: 1. I gave the div an id rather than a name 2. I gave the div a style of "display:none" so that it's hidden at every startup of the view

    <%# = image_tag "DownArrow.jpg" options = { onclick=page["vendor_droplist"].show } %>     <br>     <div id="vendor_droplist", style="display:none">   <%= select_tag "test",         options_for_select(@current_vendors.collect { |v| v.nickname }),         {:multiple => true} %>     </div>

OK, I got rid of all the syntax errors. The vendor_drop list is now hidden on startup of the view I switched from an image to a button However, the drop list does not get displayed when the ShowList button is clicked Code is below. Ideas are most welcome

    <%= button_to_function("ShowList", %<page["vendor_droplist"].show> ) %>     <br>     <div id="vendor_droplist", style="display:none">   <%= select_tag "test",         options_for_select(@current_vendors.collect { |v| v.nickname }),         {:multiple => true} %>     </div>

I don't know whether it is still relevant after your later emails but the problem here is (at least in part) that you are missing a comma after "DownArrow.jpg" and a > after options =

Colin

OK, I got rid of all the syntax errors. The vendor_drop list is now hidden on startup of the view I switched from an image to a button However, the drop list does not get displayed when the ShowList button is clicked Code is below. Ideas are most welcome

<%= button_to_function("ShowList", %<page["vendor_droplist"].show> ) %>

Have you checked the html generated by the above to make sure it is generating what you expect?

Colin

Hi Colin,

Your "check the HTML" tip is *great* ... it's now in my debugging arsenal.

comma missing

That was a tip on my img-base code. I'll get back to that because I like the down-arrow image better that ShowList button version, which is close to working.

My last next-to-last problem in this code is that the ShowList button doesn't toggle the visibility of the vendor_droplist. My guess is that the onclick value is RJS that should have been translated into JavaScript at this point.

My final problem is how to add on-click code to the return vendor items so that clicking any of them toggles (or hides: same effect in this context) the vendor_droplist. But I can probably do this on my own correctly.

Below is the ERB code and the generated HTML for the vendor input. As I've done so often, thank you for looking into my problem. I am learning this stuff steadily so I should post questions much less frequently in just a few weeks.

Best wishes, Richard

Vendor input code

Another tip is to install the Html Validator add-on to Firefox and it will automatically validate your pages as you develop, this often shows up the reason why something is not working as you expect.

Colin

Hey Colin,

Your "how's the HTML" question really did the job: <div> tag not allowed within the scope of a <form> tag.

Which means the my .erb can't have:

<% form_for(@expense) do |f| %>     [snip]     <div id="vendor_droplist" style="display:none">   <%= select_tag "test",         options_for_select(@current_vendors.collect { |v| v.nickname }),         {:multiple => true} %>     </div>

More importantly to me, it means that:

    <input onclick="page.toggle :vendor_droplist;" type="button" value="ShowList" />

won't work inside that form.

I'm going to post a new question on how to fix this while I scour the documentation to figure it out myself.

Again, thank you for sharing your expertise with me, Richard

What gives you that idea? It's simply not true.

Hi Hassan,

Thanks for joining the conversation. I ran my the HTML page which was generated by my .erb. I had a form_for with an embedded div and the HTML page had an embedded div. W3C didn't like it. The details are at http://www.pastie.org/881730. Now that I look at it again, perhaps the Object tag in place of the Div will work.

Best wishes, Richard

It's not that you have a <form> with a nested <div> that the W3C is complaining at, it's that you have a <p> with a nested <div> - lines 33 and 39 of your NewExpensePage.htm

Read line 11 of your validation pastie -- that is almost certainly your problem.

Getting used to reading the HTML DTD(s) would be a good idea :slight_smile:

Great response, Michael. I now have a clean validation on the compiled code, but I've still got a problem.

I've got clean code that validates. http://www.pastie.org/882256 displays: - new.html.erb - HTML generated for new.html.erb - Validation for generated html, which has one spurious (IMHO) warning

The code that's problematic for me is:   <%# This begins the vendor-name code %>     <%= f.label :vendor %><br />     <%= f.text_field :vendor %>     <%= button_to_function("ShowList", %<page["vendor_droplist"].toggle> ) %>     <br />     <div id="vendor_droplist" style="display:none">       <%= select_tag "list",       options_for_select(@current_vendors),       :multiple => true, :size => 3 %>     </div>   <%# This is the end of the vendor-name code %>

The vendor_droplist without the style allows vendors to be displayed in a drop-down-style list. With the "hide style", I expected that clicking the ShowList button would cause the list to be displayed, but that does not happen.

Do you any further idea about how to get that button working?

Thanks in Advance for any other ideas you may have.

Hi Hassan, please take a look at my reply to Michael.