Toggling a DIV based on a menu selection

I have a situation where when the user selects a state for a model by
choosing a particular menu item in the view, I want to either show or
hide a DIV containing other relevant attribute choices… pretty basic
stuff, but I’m still a newbie on a lot of the rails view/ui stuff.

I think I need a combination of observe_field on the attribute being
changed by the menu and then some kind of Prototype toggle effect. And
I also need to check the match the user’s menu choice against a
condition that says whether or not to show the secondary set of
choices in the DIV.

Can anyone show me what the code fragment would look like to do this?

TIA

lunaclaire,

how about

In the view:

this is the text to hide
Click toggle to hide the text: <%= link_to_remote "Toggle", :url=>{:action=>:toggle} %>

In the controller:

def toggle
render :update do |page|
page.hide ‘test_div’
end
end

That is very simplistice but it may get you started. If you want to
toggle both ways then you need to be a bit smarter. From your
description, I suspect you actually want a drop down select box. So
you will need to use observe field to call a method that will check
the selected value and render hide or show accordingly.

hth

tonypm

On 9/24/07, tonypm [email protected] wrote:

Click toggle to hide the text:
<%= link_to_remote “Toggle”, :url=>{:action=>:toggle} %>

Whoa, no need for a round-trip to the server just to toggle a div.
This will do the toggle on the client side:

<%= link_to_function ‘Toggle’, ‘Element.toggle(“test_div”)’ %>

Element.toggle is a Prototype function.

On 9/24/07, Dave S. [email protected] wrote:

Bob S. wrote:

Whoa, no need for a round-trip to the server just to toggle a div.

Certainly not normally, but one of the OP’s requirements was:
“And I also need to check the match the user’s menu choice against a
condition that says whether or not to show the secondary set of
choices in the DIV.”

Indeed. I failed to read the OP carefully. Thanks.

Bob S. wrote:

On 9/24/07, tonypm [email protected] wrote:

Click toggle to hide the text:
<%= link_to_remote “Toggle”, :url=>{:action=>:toggle} %>

Whoa, no need for a round-trip to the server just to toggle a div.
This will do the toggle on the client side:

<%= link_to_function ‘Toggle’, ‘Element.toggle(“test_div”)’ %>

Element.toggle is a Prototype function.

Certainly not normally, but one of the OP’s requirements was:
“And I also need to check the match the user’s menu choice against a
condition that says whether or not to show the secondary set of
choices in the DIV.”

In my opinion, though, you would be better off incorporating that logic
in the original request. (Of course, I’m a newb.)

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