selection box update

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

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.

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'); " %> <div id="div_test"> - test - </div>

regards, Stijn

Hi there,

You have 2 options:

Option 1 - No Ajax (tested): <!-- Start HTML --> MY TEST

<div id="one" style="display:none">I am div one<br/><%= render :partial => 'one' %></div> <div id="two" style="display:none">I am div two<br/><%= render :partial => 'two' %></div>

<%= select_tag "test", options_for_select([["please select an option...", ""], ["show one", "one"], ["show two", "two"]]), :onchange => "$(this.value).show()" %>

<!-- End HTML -->

Option 2 - Ajax(not tested):

<div id="update_me"></div> <%= 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