# Selection box update

**URL:** <https://www.ruby-forum.com/t/selection-box-update/159231>\
**Category:** Rails\
**Created:** [February 2, 2009, 5:02pm UTC](https://www.ruby-forum.com/t/selection-box-update/159231 "2009-02-02T17:02:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 2, 2009, 5:02pm UTC](https://www.ruby-forum.com/t/selection-box-update/159231/1 "2009-02-02T17:02:28Z")

</div>

Hi all,

Depending on the selection in a selection box I need to render a  
partial. I want to do this without calling the server. I think I need  
to use link\_to\_function. The problem is that I dont see how to  
implement this for a selection box. I also what to trigger the partial  
rendering immediately after the user made a selection in the box (and  
thus not clicking on a button)

Thanks,  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 2, 2009, 8:19pm UTC](https://www.ruby-forum.com/t/selection-box-update/159231/2 "2009-02-02T20:19:24Z")

</div>

Whatever you wanted to put in the link\_to\_function, put in the  
onchange of the select. Note that since you don’t want to make the  
round trip to the server to render the partial, or update a div with  
the contents of the partial, whatever you want to display as a result  
of the user selecting something in the select box, needs to be already  
on the page. You’ll just show/hide divs depending on the user  
selection.

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 3, 2009, 4:34pm UTC](https://www.ruby-forum.com/t/selection-box-update/159231/3 "2009-02-03T16:34:58Z")

</div>

Thanks for the reply.

I wrote a small test but I don’t see how I can render a partial when  
the onchange event is triggered. Don’t I have to use RJS for this?

\<% options = [“wedstrijd”, “mededeling”] %\>  
\<%= select\_tag “test”, options\_for\_select(options),  
:onchange =\> "Element.hide(‘div\_test’); " %\>

- test -

regards,  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 3, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/selection-box-update/159231/4 "2009-02-03T21:09:01Z")

</div>

Hi there,

You have 2 options:

Option 1 - No Ajax (tested):

MY TEST

I am div one  
\<%= render :partial =\> 'one' %\>

I am div two  
\<%= render :partial =\> 'two' %\>

\<%= select\_tag “test”, options\_for\_select([[“please select an  
option…”, “”], [“show one”, “one”], [“show two”, “two”]]), :onchange  
=\> “$(this.value).show()” %\>

Option 2 - Ajax(not tested):

\<%= select\_tag "test", options\_for\_select([["please select an option...", ""], ["show one", "one"], ["show two", "two"]]), :onchange =\> "Ajax.Request("/some\_controller/some\_action/?div\_id=" + this.value %\>

In your controller you could then do something like:

def some\_action

# do stuff

render :update do |page|  
page[‘update\_me’].replace\_html :partial =\> param[:div\_id]  
page[params[:div\_id].show  
end  
end

Instead of the render :update block, you can have a respond\_to |  
format| block and put a format.js {} in there, create an rjs template  
with the name of the action, and paste the code inside the  
render :update block in it.

Cheers,  
Ahmed
