Hi, How do I link two HTML SELECT''s together. Let''s say for example that I have a drop-down list with a list of all countries. Once the user selects one of the countries I would like to make an AJAX call back to the server to populate the second drop-down list with all of the cities for that country. Any ideas on how to go about to do this? Regards Jerrie Pelser -- Posted via http://www.ruby-forum.com/.
Look at observe_field in the API. Basically, you have a select box with your countries, an observe field that watches that select box and retrieves a new select box which is placed in a div that you specify. Really easy actually (and that''s why I love this framework!) -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Jerrie Pelser Sent: Monday, March 20, 2006 1:27 PM To: rails@lists.rubyonrails.org Subject: [Rails] Linking two HTML SELECT''s together Hi, How do I link two HTML SELECT''s together. Let''s say for example that I have a drop-down list with a list of all countries. Once the user selects one of the countries I would like to make an AJAX call back to the server to populate the second drop-down list with all of the cities for that country. Any ideas on how to go about to do this? Regards Jerrie Pelser -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hogan, Brian P. wrote:> Look at observe_field in the API. > > Basically, you have a select box with your countries, an observe field > that watches that select box and retrieves a new select box which is > placed in a div that you specify. Really easy actually (and that''s why I > love this framework!)Brian and Jerrie, we are talking on this subject in the "Collection_select''s linked" topic. Brian, I tried to use observer_fiel, but I am not obtaining to show to the list select_tag of first select. How you are making? Eleudson -- Posted via http://www.ruby-forum.com/.
Here''s what I did... Very hastily hacked from an existing program:
View page:
<!-- show list of projects in the system -->
<select id="test" name="task[project_id]">
<%=options_from_collection_for_select(@projects, "id",
"name",
params[''pid''].to_i )%>
</select>
<!-- observe projects list and get the list of assigned team members ->
<%= observe_field("test",
:update => "output_field",
:url => {:action => "ajax_project_members"}
)
%>
<!-- display output here -->
<span id="output_field">
Select a project.
</span>
controller:
def ajax_project_members
res = ""
p = Project.find(id, :include=>[:teammembers]) rescue nil
if p.nil?
res = ''Choose a project''
else
res += "<select name="task[assigned_to]">
res += options_from_collection_for_select p.teammembers, "id",
"name"
res +="</select>"
end
return result
end
Looking at this now, I notice it could be done a little differently (you
could just use <%select %> instead of options_from_collection... But
this still works. (this was originally for a form that had multiple
tasks that you could move to a different project.)
-----Original Message-----
From: rails-bounces@lists.rubyonrails.org
[mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Eleudson
Queiroz
Sent: Monday, March 20, 2006 2:21 PM
To: rails@lists.rubyonrails.org
Subject: [Rails] Linking two HTML SELECT''s together
Hogan, Brian P. wrote:> Look at observe_field in the API.
>
> Basically, you have a select box with your countries, an observe field
> that watches that select box and retrieves a new select box which is
> placed in a div that you specify. Really easy actually (and that''s
why
> I love this framework!)
Brian and Jerrie, we are talking on this subject in the
"Collection_select''s linked" topic.
Brian, I tried to use observer_fiel, but I am not obtaining to show to
the list select_tag of first select. How you are making?
Eleudson
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails