Forum: Ruby on Rails selection box update

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
82a578052d304559f15180bdf56555d2?d=identicon&s=25 Tarscher (Guest)
on 2009-02-02 17:02
(Received via mailing list)
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
8d38199a01d2243e313c48456c6ddff9?d=identicon&s=25 Daly (Guest)
on 2009-02-02 20:19
(Received via mailing list)
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.
82a578052d304559f15180bdf56555d2?d=identicon&s=25 Tarscher (Guest)
on 2009-02-03 16:34
(Received via mailing list)
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
8d38199a01d2243e313c48456c6ddff9?d=identicon&s=25 Daly (Guest)
on 2009-02-03 21:09
(Received via mailing list)
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
This topic is locked and can not be replied to.