Forum: Ruby on Rails Dynamic Select Box

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.
82fc129ad085e61bb6351515524ba00c?d=identicon&s=25 Karl Wheeler (saboteur)
on 2006-04-12 12:35
Hi,

I'm new to rails/ruby having come from PHP and am just starting to get
my head round how easy it can be :)

However, can anyone point me in the right direction for dynamicaly
updating a select box based upon the choice of a previous select box,
without a page refresh.

Any and all help greatly appreciated.

Thanks
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-04-12 13:23
(Received via mailing list)
Using RJS calls with an observe_field is the way to go.  Works great in
FF
but you cannot do this in IE by using RJS alone.  The reason is that RJS
uses the innerHTML method to replace content with a page.html_replace
call.
IE does not support the innerHTML call on select boxes.  You would need
to
implement it in javascript completely.

The only way that I can think of (maybe) is to use a javascript array
and
update the array from an RJS call.

Cheers
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-04-12 14:56
(Received via mailing list)
Liquid wrote:
> Using RJS calls with an observe_field is the way to go.  Works great in
> FF but you cannot do this in IE by using RJS alone.  The reason is that
> RJS uses the innerHTML method to replace content with a
> page.html_replace call.  IE does not support the innerHTML call on
> select boxes.  You would need to implement it in javascript completely.
Or rip out the whole select box and replace it, rather than just the
option list.

--
Alex
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2006-04-12 18:16
(Received via mailing list)
My approach was exactly that....

My select box was monitored by an observer.. The observer called another
action that rendered the partial for the second select box.
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-04-13 02:02
(Received via mailing list)
I did try to rip out the entire select box and replace it, but for some
reason that I still havn't worked out, once I replaced the select box,
the
observe_field didn't work anymore so my next box (I had 3) was never
going
to update.
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-13 02:48
(Received via mailing list)
On Apr 12, 2006, at 5:02 PM, Liquid wrote:

> I did try to rip out the entire select box and replace it, but for
> some reason that I still havn't worked out, once I replaced the
> select box, the observe_field didn't work anymore so my next box (I
> had 3) was never going to update.
>

Here is one way that we have been doing dynamic select boxes. Maybe
this helps you a bit.


----The first select box --------
       <span class='formw' style='width: 70%;'>
         <%= select_tag( :model_year,
                         options_for_select( @model_years, session
[:PersVeh].model_year ),
                         :onchange => remote_function( :update =>
'NOTUSED',
                                                       :with =>
"'model_year='+value",
                                                       :loading =>
"Element.show('loading-indicator1')",
                                                       :complete =>
evaluate_remote_response,
                                                       :url =>
{ :action => :vehicle_select_make } ) ) %>
         <%= image_tag('indicator.gif',
                       :id    => 'loading-indicator1',
                       :style => 'display:none;' ) -%>
       </span>



----submits to an action that fills the variables and then does
render :layout => false----

----Here is what the action renders------

<%= update_element_function('vehicle_makes',
                             :content => render( :partial =>
'vehicle_select_make' ) ) %>
Element.hide('loading-indicator1');
Element.show('vehicle_makes');
<%= update_element_function('button-group',
                             :content => render( :partial =>
'vehicle_buttons' ) ) %>




So basically the trick is with the  :complete =>
evaluate_remote_response in the select tags. This makes the results
of the request get eval'ed in JS.

Hope that helps a bit.

Cheers-
-Ezra
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-04-13 02:54
(Received via mailing list)
Thanx Ezra...

Is there some documentation for evaluate_remote_response?  Is this a
ruby/rails method or a JS function?  I'm not familiar with this one.

Cheers
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-13 03:21
(Received via mailing list)
I'm not sure if there is any docs for that ;) I found it a long time
ago by accident and I don't remember seeing much docu for it.

	Ohh wait here it is!

http://rubyonrails.com/rails/classes/ActionView/Helpers/
PrototypeHelper.html#M000419


Cheers-
-Ezra
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-04-13 03:24
(Received via mailing list)
Cool ...  Thanx I'll give it a try..
Ec5ea44b42812e61857a5646b4109194?d=identicon&s=25 andre hartawan (Guest)
on 2006-04-13 04:07
(Received via mailing list)
can i use ruby to develope mobile application

andre

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
F3dc06f587d1ff4c7366b102bfda9204?d=identicon&s=25 David Mitchell (Guest)
on 2006-04-13 04:28
(Received via mailing list)
Yes

Dave M.
82fc129ad085e61bb6351515524ba00c?d=identicon&s=25 Karl Wheeler (saboteur)
on 2006-04-13 10:48
Thanks Ezra. That's just what i needed

Karl
38dedc296b7207c2a0d59fd3aac906b4?d=identicon&s=25 Ramanan (Guest)
on 2006-08-19 00:00
Daniel ----- wrote:
> Cool ...  Thanx I'll give it a try..

Were  you able to imlement the dynamic select box?  I have a similar
requirement to populate City based on State and then Locality based on
City.

If yes, could you please email me the code or paste the code here.

Thx.
Ramanan
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Kevin Olbrich (Guest)
on 2006-08-19 00:45
(Received via mailing list)
On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:
>Ramanan
>
>
>--
>Posted via http://www.ruby-forum.com/.
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails

Take a look at this...

http://www.sciwerks.com/blog/2006/07/07/updating-s...

_Kevin
www.sciwerks.com
38dedc296b7207c2a0d59fd3aac906b4?d=identicon&s=25 Ramanan (Guest)
on 2006-08-19 01:01
Kevin Olbrich wrote:
> On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:
>>Ramanan
>>
>>
>>--
>>Posted via http://www.ruby-forum.com/.
>>_______________________________________________
>>Rails mailing list
>>Rails@lists.rubyonrails.org
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>
> Take a look at this...
>
> http://www.sciwerks.com/blog/2006/07/07/updating-s...
>
> _Kevin
> www.sciwerks.com


I have a working code where there are only 2 select boxes i.e.
populating city when state is selected.  I am having issues with
populating locality based on selected city.  Working with 2 dynamic
select is straight forward.  Working with more than 2 dynamic/cascading
select box is a real issue.

Thx. for your help
761b4595d11fb29f8bd21cf0e7788db3?d=identicon&s=25 Iw (Guest)
on 2006-09-01 15:30
I am experiencing problems retrieving information from the related drop
downs.
I can save information in the database (i.e. state_id and city_id  are
getting saved in the Address table).

 However on the  Address's edit/update screen, the information from the
first "State" drop down is getting retrieved
properly but nothing is getting displayed in the second drop down -
"City".

I am using observe fields to retrieve data for the second drop down.
Please advise how to prepopulate information for the second drop down
 based on the "id" stored in the database on Edits screens.

Thanks in advance for your help!

I


Ramanan wrote:
> Kevin Olbrich wrote:
>> On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:
>>>Ramanan
>>>
>>>
>>>--
>>>Posted via http://www.ruby-forum.com/.
>>>_______________________________________________
>>>Rails mailing list
>>>Rails@lists.rubyonrails.org
>>>http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>> Take a look at this...
>>
>> http://www.sciwerks.com/blog/2006/07/07/updating-s...
>>
>> _Kevin
>> www.sciwerks.com
>
>
> I have a working code where there are only 2 select boxes i.e.
> populating city when state is selected.  I am having issues with
> populating locality based on selected city.  Working with 2 dynamic
> select is straight forward.  Working with more than 2 dynamic/cascading
> select box is a real issue.
>
> Thx. for your help
5f23b06d22b84ec6c902bf390071a79c?d=identicon&s=25 _Kevin (Guest)
on 2006-09-01 16:20
(Received via mailing list)
Iw wrote:
> I am using observe fields to retrieve data for the second drop down.
> >> On Saturday, August 19, 2006, at 12:00 AM, Ramanan wrote:
> >> Take a look at this...
> > select is straight forward.  Working with more than 2 dynamic/cascading
> > select box is a real issue.
> >
> > Thx. for your help
>
>
> --
> Posted via http://www.ruby-forum.com/.

You'd have to give us a bit more information about how you are
populating your select in the first place.

_Kevin
761b4595d11fb29f8bd21cf0e7788db3?d=identicon&s=25 Iw (Guest)
on 2006-09-01 16:58
Here is relevant code:
The "city" in the second dropdown is not getting selected in Edit
screen.

Thank you so much for you help!
------------------------------
_form.rhtml


	<%= collection_select(:address, "state_id", State.find(:all), :id,
:state) %>
	<p id="address_city_container"><%= render :partial => 'select_city'
%></p>

	        <%= observe_field("address_state_id",
	                :frequency => 0.25,
	                :update => "address_city_container",
	                :url => {:controller => 'address', :action =>
:filtered_city_select},
	                :with => "'state_id=' + value")%>
<% --------
	- address_controller.rb

 def filtered_city_select

   @cities = City.find_all_by_state_id(@params["state_id"])
    render :partial => 'select_cities'
 end



 def edit
   @address = Address.find(params[:id])
   @states = State.find_all
   @cities = City.find(:all, :conditions =>[ "state_id = ?",
@address.state_id])

 end


 %>

------
partial _select_cities.rhtml

<% if @cities %>
<select name="address[city_id]" id="address_city_id">
     <option value="">-- Select City --</option>
 	<%=
		 options_from_collection_for_select(@cities, "id", "city")
	%>
 </select>
<% end %>
5f23b06d22b84ec6c902bf390071a79c?d=identicon&s=25 _Kevin (Guest)
on 2006-09-01 17:30
(Received via mailing list)
Iw wrote:
> :state) %>
> 	- address_controller.rb
>    @address = Address.find(params[:id])
> partial _select_cities.rhtml
> --
> Posted via http://www.ruby-forum.com/.

Couple of points.
1. if you set the frequency of your observer to 0, it should fire off
whenever the value changes, and not every 0.25 seconds.

2. check your log files to see if you are getting errors thrown by the
filtered_city_select method

3. Don't use '@params', it's deprecated.

Does it work at all?

_Kevin
761b4595d11fb29f8bd21cf0e7788db3?d=identicon&s=25 Iw (Guest)
on 2006-09-01 18:03
_Kevin wrote:
> Iw wrote:
>> :state) %>
>> 	- address_controller.rb
>>    @address = Address.find(params[:id])
>> partial _select_cities.rhtml
>> --
>> Posted via http://www.ruby-forum.com/.
>
> Couple of points.
> 1. if you set the frequency of your observer to 0, it should fire off
> whenever the value changes, and not every 0.25 seconds.
>
> 2. check your log files to see if you are getting errors thrown by the
> filtered_city_select method
>
> 3. Don't use '@params', it's deprecated.
>
> Does it work at all?
>
> _Kevin



No errors in the log.
Insert works. Data is getting saved in the database. The Edit/Update
screen does not highlight (select) the item in the second drop down -
the city.
03d5be82d3c41ff7edd0e20a3569ca06?d=identicon&s=25 Dan Miller (Guest)
on 2006-10-11 17:14
You can do it with mostly javascript.

Here is an Ajax 'double select' widget, in object-oriented,
cross-browser JavaScript. Very easy to plug and play:

http://www.webonweboff.com/widgets/ajax/ajax_linke...

Article includes a demo, source code, explanations. The page that
actually retrieves the db data will need to be in your web language of
choice, of course.

Hope this helps...
Db2175eb86669d396b8596d11478f5ef?d=identicon&s=25 Cayce Balara (yardboy)
on 2006-10-11 18:06
Another option is to observe and redraw the entire form, rather than
just field-by-field.

I have my entire form in a partial, and I use RJS to redraw the partial
from an observe_form event - anytime anything changes in the form, the
entire form is redrawn and this accomplishes 2x, 3x, Nx select box
updates.

c.


Ramanan wrote:
>
> I have a working code where there are only 2 select boxes i.e.
> populating city when state is selected.  I am having issues with
> populating locality based on selected city.  Working with 2 dynamic
> select is straight forward.  Working with more than 2 dynamic/cascading
> select box is a real issue.
>
> Thx. for your help
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2006-11-07 11:00
(Received via mailing list)
On 8/19/06, Ramanan <nramanan@hotmail.com> wrote:
> Thx.
> Ramanan
>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

I did end up with something...  You can find it at
http://rails.techno-weenie.net/tip/2006/6/20/casca...

But this is far from perfect.  Kevins has given an alternative solution
as
well, but the one that I'm after I can't seem to find.

Shortly after I posted the tip on
http://rails.techno-weenie.netTechnoweenie, aka Rick Olson posted a
much better solution.  Since then
there has been some db issues at rails weenie and I can't find it.

Rick if you catch this post can you please provide a link to the tip you
posted.

Cheers
This topic is locked and can not be replied to.