Selection box update


#1

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


#2

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.


#3

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


#4

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